Module: @accessors-modules/http-server

Module supporting web servers. This module defines one class, HttpServer, which runs a web server on the given port and host interface (e.g. localhost).

HttpServer generates two events: A listening event after the server has been started and is ready, and Request events for each incoming request. The request event includes the request data plus a requestID number.

Accessors should provide a complete response back to HttpServer and include the matching requestID.

Version:
  • $$Id$$
Author:
  • Edward A. Lee ad Elizabeth Osyk
Source:

Methods

(static) HttpServer(options)

Construct an instance of HttpServer. After invoking this constructor (using new), the user script should set up listeners and then invoke the start() function on this HttpServer. This will create an HTTP server on the local host.

The options argument is a JSON object containing the following optional fields:

  • hostInterface: The IP address or name of the local interface for the server to listen on. This defaults to "localhost", but if the host machine has more than one network interface, e.g. an Ethernet and WiFi interface, then you may need to specifically specify the IP address of that interface here.
  • port: The port on which to listen for requests (the default is 80, which is the default HTTP port).
  • timeout: The time in milliseconds to wait after emitting a request event for a response to be provided by invoking the respond() function. This is a long that defaults to 10,000. If this time expires before respond() is invoked, then this module will issue a generic timeout response to the HTTP request.

This subclasses EventEmitter, emitting events:

  • listening: Emitted when the server is listening.
  • request: Emitted when an HTTP request has been received.

    A request event contains an object with fields for the requestID, method, path, and body (if any). If there is no body, the body field is absent.

    A typical usage pattern looks like this:

    var httpServer = require('httpServer'); var server = new httpServer.HttpServer({'port':8082}); server.on('listening', function () {

     console.log('Server is listening.');

    }); server.on('request', function (request) {

     console.log('Server received request: ' + util.inspect(request));
     server.respond(request.requestID, 'Hello World');

    }); server.start();

    where onListening is a handler for an event that this HttpServer emits when it is listening for requests.

Parameters:
Name Type Description
options

The options.

Source: