-
Notifications
You must be signed in to change notification settings - Fork 1
HttpServer
ricardoboss edited this page Nov 19, 2023
·
1 revision
Starts a HTTP server on the specified port/with the specified options and calls the callback for each incoming request.
httpServer(number port, function requestHandler)
httpServer(map options, function requestHandler)
-
port
must be an integer (number) between 1 and 65535 -
options
can contain the following key-value-pairs:-
port
: integer (number) between 1 and 65535 -
headers
: a map of default headers to include in the response
-
-
requestHandler
must be a function that takes a map as its only argument
- the
requestHandler
function is called for each incoming request - the return value of the
requestHandler
function is used as the response body, except if it is a map- if the return value is a map, the following keys are used to construct the response:
-
status
: integer (number) between 100 and 599 -
headers
: a map of headers to include in the response -
body
: the response body
-
- if the return value is not a map, the following response is constructed:
-
status
: 200 -
headers
: a map containing the default headers and aContent-Type
header with the valuetext/plain
-
body
: the return value converted to a string usingtoString
-
- if the return value is a map, the following keys are used to construct the response:
The simplest server just returns "Hello World!" for each request:
httpServer(8080, (map request) {
return "Hello World!"
})
A more complex server that returns a JSON response:
httpServer(8080, (map request) {
return {
"status": 200,
"headers": {
"Content-Type": "application/json"
},
"body": {
"message": "Hello World!"
}
}
})
Using the options
map:
httpServer({
"port": 8080,
"headers": {
"Server": "STEP"
}
}, (map request) {
return {
"headers": {
"Content-Type": "application/json"
},
"body": {
"message": "Hello World!"
}
}
})
© 2023 Ricardo Boss • Licensed under MIT • Want to update this page? Edit it on GitHub and submit a pull request!