Accessor: net/WebServer

net/WebServer

This accessor starts a server that listens for HTTP requests on the specified hostInterface and port and issues responses. The hostInterface specifies what network interface (e.g. Ethernet, WiFi, or localhost) to use. The default is 'localhost', which (usually) means that the server listens only for requests coming from the local machine. This is useful for testing. To listen for requests on all IPv4 addresses on the local machine, specify '0.0.0.0'. This will make the server accessible to any machine that has access to an IP address for the local machine.

When this server receives an http requests from the network (or from the local machine), it produces a JavaScript object on the output that includes at least the following properties:

  • body: The body of the request, or null if there is no body.
  • method: A string that describes the HTTP method of the request, which may be "GET", "PUT", etc.
  • path: The path in the URL of the request, such as "/" when there is no path.
  • requestID: An identifier for the request.

To produce a response, this accessor waits for an input on its response port that is a JavaScript object containing the following properties:

  • requestID: An identifier matching a request for which a response has not already been issued.
  • response: The body of the response, such as HTML to display to the requester or a JavaScript object with a JSON representation (the JSON representation will be sent back as the response). If this property is not included in the input, then the input will be stringified as a JSON object and sent. This will include the requestID property.

If there is no pending request with a matching ID, then an error will be issued.

A simple use case is to connect the request output to some other actor that generates a response, and then to feed that response back to the response input. Be sure to include the requestID property in the response.

When wrapup() is invoked, this accessor closes the server.

This accessor requires the module httpServer.

Version:
  • $$Id$$
Author:
  • Edward A. Lee amd Elizabeth Osyk
Source:
Inputs:
Name Type Description
response The response to issue to a request.
shutdown Shutdown the web server.
Outputs:
Name Type Description
listening int When the server is listening for connections, this output will produce the port number that the server is listening on
request The request that came into the server.
Parameters:
Name Type Description
hostInterface string The IP address or domain name of the network interface to listen to.
port int The port to listen on.

Methods

(static) initialize()

Starts the server.

Source:

(static) setup()

Sets up the accessor by defining inputs and outputs.

Source:

(static) wrapup()

Removes all inputHandlers from sockets.
Unregisters event listeners from sockets.
Closes server.

Source: