HTTP server

A built-in HTTP server can be used to listen for incoming connections and respond to requests. After creating a new server object, any number of request handlers can be added. The first handler matching the requested URL will be used for the response. var server = new dev.http.Server; server.addFolder (null, null, 'my_public_files'); // serve all requests using files in the my_public_files folder server.listen(); Handlers can be used to: respond to a request by sending a file; respond by generating a page from an EJS or PHP template; call a function to generate any kind of response; or modify the request and pass it to the next handler. Each request is handled by a separate thread. The server object will stop listening when there are no more references to it (the page is destroyed).

This server is not yet secure or reliable enough to serve Internet web pages, and should only be used for communication between apps. For security, files can only be served from within the app folder tree. Future versions will support HTTPS, HTTP 2, websockets, and other cool stuff.

Properties of a dev.http.Server object

server.port

server.backlog

server.listening

server.timeout

Methods of a http server object

The first few methods are used to add new request handlers. The first handler that matches a request will be used for the response.

If the host argument is null then it will match all requests, otherwise it must match the Host field in the request header. If the path argument is null then it will match any requested path, otherwise it must match the requested path. The standard wildcards ? and * can be used in either argument to match any single character or group of characters. For example... server.addFolder ("*my.com*", "/my_files/*", "public");

...matches any subdomain or port of my.com, requesting anything in the /my_files folder, and serves it from the app's /public subfolder.


server.addFile (host, path, local_file)

server.addFile (host, path, local_file, variables)

server.addFolder (host, path, local_folder)

server.addFunction (host, path, function)

server.clear ()

server.listen ()
server.listen (port)

server.stop ()

Methods of a dev.http.Request object

If an addFunction handler matches a request, its function will be passed a dev.http.Request object as an argument. function RequestHandler (request){/* handle the request here */}

request.method

request.path

request.protocol


request.getHeader (field_name)

request.setHeader (field_name, value)

Methods of a dev.http.Response object

If an addFunction handler matches a request, its function may handle the request by returning a dev.http.Response object. function RequestHandler (request){ var response = new dev.http.Response (200); // OK // setup the response here return response;}

response.body

response.protocol

response.status


response.getHeader (field_name)

response.setHeader (field_name, value)

Example

Each incoming connection to this server is processed by a series of handlers covering: moved folders, the home page, queries using clean URLs, normal file serving, page statistics, and a custom 404 page. <script> var stats = { hits: 0 }; var server = new dev.http.Server; // modify request to redirect moved folders server.addFunction (null, "/old_folder/*", function (request){ request.path = request.path.replace ("/old_folder", "/new_folder"); return request;}); // API for page statistics server.addFunction (null, "/stats", function (request){ var response = new dev.http.Response (200); response.setHeader ("Content-Type", "application/json"); response.body = JSON.stringify (stats); return response;}); // count page hits server.addFunction (null, null, function (request){ ++stats.hits;}); // setup home page server.addFile (null, "/", "public/index.html"); // use PHP to respond to queries like /meetings/mary/july server.addFile (null, "/meetings", "scripts/meetings.php", "user, month"); // the query becomes scripts/meetings.php?user=mary&month=july // general requests for files in public folder server.addFolder (null, null, "public"); // uncount files not found server.addFunction (null, null, function (request){ stats.hits--;}); // show custom file not found page server.addFile (null, null, "public/404.html"); server.listen (); </script>