Below is a breakdown of the Server
component which is an extended Router
instance for modularity support.
- See
> [Router]
for more information on additional methods and properties available.
key_file_name
[String
]: Path to SSL private key file to be used for SSL/TLS.- Example:
'misc/key.pm'
- [Required] for an SSL server.
- Example:
cert_file_name
[String
]: Path to SSL certificate file.- Example:
'misc/cert.pm'
- [Required] for an SSL server.
- Example:
passphrase
[String
]: Strong passphrase for SSL cryptographic purposes.- Example:
'SOME_RANDOM_PASSPHRASE'
- Optional for an SSL server.
- Example:
dh_params_file_name
[String
]: Path to SSL Diffie-Hellman parameters file.- Example:
'misc/dhparam4096.pm'
- Optional for an SSL server.
- Example:
ssl_prefer_low_memory_usage
[Boolean
]: Specifies uWebsockets to prefer lower memory usage while serving SSL requests.auto_close
[Boolean
]: Specifies whether theServer
instance should automatically be closed when process exits.- Default:
true
- Default:
fast_buffers
[Boolean
]: Specifies HyperExpress to useBuffer.allocUnsafe
for storing incoming request body data for faster performance.- Default:
false
- Note! Any data in the unsafely allocated buffer will always be written over thus this option is provided for those working with strict regulatory requirements.
- Default:
fast_abort
[Boolean
]: Specifies HyperExpress to forcefully/abruptly close incoming request connections with bad conditions such as payload too large. This can significantly improve performance but at the cost of no HTTP status code being received by the sender.- Default:
false
- Default:
trust_proxy
[Boolean
]: Specifies whether incoming request data from intermediate proxy(s) should be trusted.- Default:
false
- Default:
max_body_buffer
[Number
]: Maximum number ofbytes
to buffer in memory before the data is consumed. Behaves similar tohighWaterMark
in Node.js streams.- Default:
16 * 1024
or 16kb
- Default:
max_body_length
[Number
]: Maximum number ofbytes
allowed for incoming request body size. For reference, 1kb = 1024 Bytes and 1mb = 1024kb.- Default:
250 * 1024
or 250kb
- Default:
streaming
[Object
]: Specifies global constructor options for internal readable and writable streams.readable
[stream.ReadableOptions
]: Constructor options forRequest
body readable streams.- See the official
> [ReadableOptions]
Node.js documentation for more information.
- See the official
writable
[stream.WritableOptions
]: Constructor options forResponse
body writable streams.- See the official
> [WritableOptions]
Node.js documentation for more information.
- See the official
- Note you can also override globally specified
streaming
options on a per-route basis in the route options.
Property | Type | Description |
---|---|---|
port |
Number |
Local listening port of this instance. |
socket |
uWS.us_listen_socket |
Listening uWS socket of this instance. |
hosts |
HostManager |
Host Manager local to this instance. |
locals |
Object |
Can be used to stores references local to this instance. |
uws_instance |
uWS.TemplatedApp |
Underlying uWebsockets TemplatedApp instance. |
routes |
Object |
All routes created on current instance. |
middlewares |
Object |
All non route specific midddlewares on current instance. |
handlers |
Object |
Global handlers for current instance. |
listen(Number: port, [String?: host, Function?: callback])
: Starts the uWebsockets server on specified port and host.listen(String: unix_path, Function?: callback)
: Starts the uWebsockets server on specified UNIX domain socket path.- Returns a
Promise
and resolvesuw_listen_socket
. - Note port or unix_path is required and host is
0.0.0.0
by default if unspecified with a port listener. - Note callback is optional and can be used as an alternative to the Promise.
- Returns a
shutdown(uws_socket?: socket)
: Performs a graceful shutdown of the server and closes the listen socket once all pending requests have been completed.- Note: listen_socket is not required.
- Returns a
Promise
and resolvesBoolean
representing whether the socket was closed successfully. - Note The server will immediately close any new incoming requests or connections while the graceful shutdown is in progress.
close(uws_socket?: socket)
: Closes the uWebsockets server instantly dropping all pending requests.- Note: listen_socket is not required.
- Returns a
Boolean
representing whether the socket was closed successfully.
set_error_handler(Function: handler)
: Binds a global catch-all error handler that will attempt to catch mostsynchronous/asynchronous errors.- Handler Parameters:
(Request: request, Response: response, Error: error) => {}
.
- Handler Parameters:
set_not_found_handler(Function: handler)
: Binds a global catch-all not found handler that will handle all requests which are not handled by any routes.- Handler Parameters:
(Request: request, Response: response) => {}
.
- Handler Parameters:
use(...2 Overloads)
: Binds middlewares and mountsRouter
instances on the optionally specified pattern hierarchy.- Overload Types:
use(Function | Router: ...handler)
: Binds the specified functions as middlewares and mounts theRouter
instances on the/
pattern.use(String: pattern, Function | Router: ...handler)
: Binds the specified functions as middlewares and mounts theRouter
instances on the specifiedpattern
hierarchy.
- Note
pattern
is treated as a wildcard match by default and does not support*
/:param
prefixes. - See
> [Router]
&> [Middlewares]
for full documentation on this method.
- Overload Types:
any(...4 Overloads)
: Creates an HTTP route on the specified pattern. Alias methods are listed below for all available HTTP methods.- Alias Methods:
all()
,get()
,post()
,put()
,delete()
,head()
,options()
,patch()
,trace()
,connect()
,upgrade()
,ws()
. - Overload Types:
any(String: pattern, Function: handler)
: Creates an any method HTTP route with the specifiedhandler
.any(String: pattern, Object: options, Function: handler)
: Creates an any method HTTP route with the specifiedoptions
andhandler
.any(String: pattern, Function: middleware, Function: handler)
: Creates an any method HTTP route with the specified route-specificmiddleware
andhandler
.any(String: pattern, Function[]: middlewares, Function: handler)
: Creates an any method HTTP route with the specified set of route-specificmiddlewares
andhandler
.
- See
> [Router]
for full documentation on this method. - See
> [Websocket]
for usage documentation on theupgrade()
andws()
alias method.
- Alias Methods:
publish(String: topic, String|Buffer|ArrayBuffer: message, Boolean?: is_binary, Boolean?: compress)
: Publishes the specified message to the specified topic in MQTT syntax to all WebSocket connections on this Server instance.- Returns a
Boolean
to signify whether the publish was successful or not.
- Returns a
num_of_subscribers(String: topic)
: Returns the number of subscribers to a topic across all WebSocket connections on this server instance.- Returns a
number
of connections.
- Returns a