Serve / HTTPServer
This module provides HTTP server functionality to
Serve.
An HTTPServer implementation in Serve will typically consist of:
- HTTPServer - The plugin instance
- HTTPInstance - A server instance
- HTTPRequest - The object containing an inbound request
- HTTPResponse - An object to manipulate the response to a request
Closing the connections in the handler isn't necessary, as this will happen automatically either due to the request itself requesting to close after a single request or the connection times out.
This interface is fairly fast, but for high-performance serving of binary files, it is recommended not to utilise a Javascript handler.
Example
const HTTP = Serve.module('HTTPServer/libHTTPServer');
var http = HTTP.create();
http.setAddress('::'); // Listen on all network interfaces on IPv6 and IPv4
http.setPort(8080);
http.setHandler( function(req, res) {
// 'req' is the HTTPRequest object
// 'res' is the HTTPResponse object
Serve.print("Incoming request for "+req.uri());
// Additionally, custom headers can be specified using res.setHeader(name, value);
res.setStatus(200);
res.setContentType('text/plain');
res.setBody('Hello, world.');
// Actual transmission to the client happens here:
res.send();
});
http.listen();
HTTPServer
HTTPInstance
HTTPRequest
HTTPResponse