-
Notifications
You must be signed in to change notification settings - Fork 1
Http Server
ricardoboss edited this page Nov 19, 2023
·
1 revision
STEP has the ability to serve HTTP requests. This can be used for creating websites and toying around with web technologies.
This script will start a server on localhost:8080 and respond to all requests with "Hello World":
httpServer(8080, (map request) {
return "Hello World"
})
The call to httpServer
is blocking, meaning any code after it is not executed.
You can also pass an existing function:
function handleRequest = (map request) {
return "Hello World"
}
httpServer(8080, handleRequest)
This script will start a server on localhost:8080 and serve files from the current
directory. It will also serve index.html
when the user requests /
:
httpServer(8080, (map request) {
if (!startsWith(request["path"], "/")) {
request["path"] = "/" + request["path"]
}
if (request["path"] == "/") {
request["path"] = "/index.html"
}
string file = "." + request["path"]
if (fileExists(file)) {
return fileResponse(file)
}
return {"body": "404 Not Found", "status": 404}
})
© 2023 Ricardo Boss • Licensed under MIT • Want to update this page? Edit it on GitHub and submit a pull request!