wilton/Server
HTTP server.
More...
Functions | |
Undefined | broadcastWebSocket (Object options, Function|Undefined callback) |
Broadcast a message to WebSocket clients. | |
Undefined | getTcpPort (Function|Undefined callback) |
Find out server TCP port. | |
Object | Server (Object options, Function|Undefined callback) |
Start HTTP server. | |
Undefined | stop (Function|Undefined callback) |
Stop HTTP server. |
This module allows to start HTTP servers and handle incoming requests using specified modules/functions.
Each value specified in views
parameter, is used both as a name of the module to handle the requests and as an URL path to route the requests to this module.
GET
, POST
, PUT
, DELETE
and OPTIONS
(for CORS support) methods are used inside the handler modules to handle incoming requests with the corresponding HTTP method.
Additionally WSOPEN
, WSMESSAGE
and WSCLOSE
WebSocket event names can be used inside hanler modules to handle incoming WebSocket events.
Server
can also serve static files (from file-system or from ZIP files) specifying documentRoots
configuration parameter.
Server
supports HTTPS (certificates must be specified) and can be configured to require client X.509
certificates.
Server
can be stopped to release system resources using stop()
method, otherwise it will be stopped during the shutdown.
Usage example:
Undefined Server::broadcastWebSocket | ( | Object | options, |
Function|Undefined | callback | ||
) |
Broadcasts the message to all the WebSocket clients currently connected on the specified path
.
May cause the broadcast message to interleave with other frames (put in the middle of continuation sequence, or in the middle to TCP fragmented sequence), if target connection is used for outbound traffic at the the time of broadcast.
List of clients to include into broadcast may be specified directly using idList
option.
options | Object configuration object, see possible options below |
callback | Function|Undefined callback to receive result or error |
Undefined
Options
String
WebSocket path to broadcast onString|Object
message to send, specified Object
will be converted to JSONArray|Undefined
optional list of WebSocket client IDs to include into the broadcast Undefined Server::getTcpPort | ( | Function|Undefined | callback | ) |
Retuns a TCP port number that this server instance is bound to.
callback | Function|Undefined callback to receive result or error |
Undefined
Object Server::Server | ( | Object | options, |
Function|Undefined | callback | ||
) |
Starts HTTP server binding the connection on a specified TCP port. Server worker threads are run in background.
options | Object configuration object, see possible options below |
callback | Function|Undefined callback to receive result or error |
Object
Serial
instanceOptions
Array
list of module names that will be used to handle incoming request; the same module names are also used as URL paths for requiests routing; functions with following names are used inside the handler module to handle the requests:GET
, POST
, PUT
, DELETE
and OPTIONS
; the following method names can be used for WebSocket events: WSOPEN
, WSMESSAGE
and WSCLOSE
; each handler function receives Request
instance as an only parameter and must use sendResponse()
, sendTempFile()
, sendMustache()
or sendWebSocket()
methods to return the response to client;Array
list of module names that will be called on the incoming request, each module must return a function(req, doFilter)
, and can either call doFilter(req)
to proceed to the next filter (and eventually - to the request view), or handle the request with sendResponse()
Number|Undefined
number of background threads to start, default value: 2
Number|Undefined
TCP port to bind to, default value: 8080
String|Undefined
IP address to bind to, default value: 0.0.0.0
Object|Undefined
HTTPS configurationString
path to SSL certificate file that must also contain a private keyString
SSL server certificate (private key) passwordString|Undefined
path to SSL CA certificate to use for validation of client X.509
certificateString|Undefined
substring of the client X.509
certificate subject that will be checked during the client certificate checkArray|Undefined
configuration for serving static filesString
URL path for this entryString|Undefined
path to file-system directory to serve files fromString|Undefined
path to ZIP file to server files fromString|Undefined
prefix to strip from ZIP paths when serving files from ZIPBoolean|Undefined
whether to load response resources through wilton_loader
; default value: falseString|Undefined
prefix (absolute path to WLIB file and optional internal path) to prepend to resources pathsNumber|Undefined
value for Cache-Control
's max-age
field to use for files server from this documentRoot
, in seconds, default value: 604800
Array|Undefined
list of MIME types mapping for this documentRoot
String
file extension (CSS
, JS
etc)String
MIME type for this extensionObject|Undefined
configuration for handling requests payloadString
path to the directory where temporary files with requests payloads will be storedNumber|Undefined
length of the generated random names for temporary files, default value: 32
Number|Undefined
max size of the request payload that is allowed to process in-memory without dumping it into file, in bytes, default value: 1048576
Object
configuration for mustache responsesArray
list of directories to use for loading mustache partial templates when returning rendered mustache templates using Request.sendMustache()
Number|Undefined
handle to access Server
instance from another thread Undefined Server::stop | ( | Function|Undefined | callback | ) |
Stops HTTP server releasing system resources, active servers will be stopped automatically on shutdown.
callback | Function|Undefined callback to receive result or error |
Undefined