Recent Changes - Search:

edit SideBar

http-server Module

Provides an HTTP server.

The WebServer accessor starts up a server on the given port and host interface, then generates an output for each incoming request. A downstream swarmlet composes and returns a response within a given timeout. WebServer generates an error response if the timeout expires before a response arrives on its input port.

The server does not currently offer:

  • Authentication. The WebServer or a separate accessor may offer this in the future.
  • Browser host. WebServer capabilites are not available in the browser host.
  • Cookies.
  • Error handling (other than timeout). This may be offered by the server or other accessor in the future.
  • MIME types other than text/html.
  • Multiple servers. Only a single server is supported.
  • Request ordering. WebServer sends responses as they arrive on its input port. The order of responses may not match the order requests were received.
  • Request queuing. WebServer generates an output as each request arrives. This may be problematic for some swarmlets.
  • Request routing. WebServer accepts requests to all paths. There may be a separate routing accessor in the future.
  • Response formatting. The downstream swarmlet is responsible for creating the response in its entirety.
  • Session management. Cookies are a prerequisite for session management.
  • Static page serving.

API

  • HttpServer(options): Create an HTTP server. Options include:
    • port : The port on which to listen for requests. Default is 80, which is the default HTTP port.
    • hostInterface: The IP address or name of the local interface for the server to listen on. Defaults to localhost. If the host machine has multiple network interfaces (e.g. Ethernet, WiFi, ...) you may need to explicitly specify hostInterface.
    • timeout: The time to wait after emitting a request for the response to arrive. In milliseconds. Defaults to 10,000.
  • respond(requestID, response,responseCode): Send response to the request with the matching requestID, optionally providing a responseCode (defaults to 200).
  • start: Start the server.
  • stop: Stop the server.
  • _request: Internal method Cape Code uses to notify that a request has arrived. Not meant to be called by Javascript.

WebServer generates these events:

  • listening: Server is ready and listening for requests.
  • request: A request has arrived. This event contains an object with fields for the requestID, method, path, body (if any), headers (if any), and params (if any). If there is no body, the body field is absent. If there are no headers or parameters, the corresponding field is set to an empty object.

Usage

First, construct an instance of HttpServer:

   var httpServer = require('@accessors-modules/http-server');
   var server = new httpServer.HttpServer({'port':8082});
   var util = require('util');  // Used for printing requests to the console.

Next, register handlers for the events:

   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');
   });

Finally, start the server:

   server.start();

Node Modules

The Node host requires three third-party modules:

Please install these prior to running a WebServer example. From /accessors/web :

   npm install express --save
   npm install helmet --save
   npm install portscanner --save

See Also

  • Cape Code demo: $PTII/org/terraswarm/accessor/demo/WebServer/WebServer.xml
  • Cape Code test cases: $PTII/ptolemy/actor/lib/jjs/modules/httpServer/test/auto
  • Node test cases: accessors-modules/web/net/test/auto
  • http-client
  • JSDoc for http-server

Back to Optional JavaScript Modules

Edit - History - Print - Recent Changes - Search
Page last modified on August 17, 2017, at 08:46 PM