Creates a request handling stack. Emits events related to the request lifecycle.
Can be called without new
.
Wraps an incoming message in a Helo request. Each stack instance has its own constructor so properties can safely be added to the prototype without affecting other stacks in the same process.
Use Stack#addRequestInitializer()
to add properties to the request as
it's instantiated.
Configures the default content types for html
, json
and form
responses. Each stack instance has its own
object so properties can safely be modified without affecting other stacks
in the same process.
The default json
implementation simply uses JSON.stringify(value)
.
The default form
implementation can only serialize strings, booleans
and finite numbers.
Registers a function which will be called on a request as it's instantiated. Initializers are called in the order in which they've been registered. They can be added even after the stack has been finalized.
Returns the stack for easy chaining.
Registers a factory method that returns a middleware function. When the stack
is finalized each factory is called in order to compute the middleware chain.
Factories are called with a single argument, being the next function in the
chain. They're expected to return a function that takes a single request
argument.
This middleware function should either return a response or call the next
function with the request
. The result of calling the next function will
always be a promise, which should be returned or used to change a response.
Cannot be called after the stack has been finalized.
Example factory:
function noop(next) {
return function(request) {
return next(request);
};
}
Returns the stack for easy chaining.
Installs a plugin: an object that may have requestInitializer
and / or
middleware
functions.
Returns the stack for easy chaining.
Computes the middleware chain, ending with a call to responder
. A stack can
only be finalized once.
Returns the stack for easy chaining.
Define a standard error response. response
must be an object with
statusCode
and chunk
properties, and optionally headers
. The response
is used based on its status code.
The 503
response is used when the response promise is rejected with an
error that has a cancel
as its name
. Similarly if the error has timeout
as its name
, the 504
response is used. For all other errors the 500
response is used.
Returns the stack for easy chaining.
Handles request
events emitted by
server
.
Returns the stack for easy chaining.
Cancels all pending response promises.
Stack