helo
Promise-based HTTP request handling stack.
Installation
npm install legendary
npm install helo
This module has a peer dependency on Legendary.
Usage
See API Docs.
Request handling
Helo handles requests a little differently than a standard Node HTTP server. To see how, let's start with a standard example:
{ ;} var server = ;server;
The application receives a req
argument, the incoming message, and a
res
argument, the outgoing message. It writes directly to the outgoing
message.
Here's Helo's equivalent:
var delay = timeddelay; { return ;} var server = ; ; server;
Responses
The application does not receive the outgoing message, instead it's expected to return a (promise for a) response object. This is then written to the outgoing message by Helo. Read more about the response objects.
The promise is cancelled if the outgoing message is closed before the promise has fulfilled. This allows the application to decide whether to continue handling the request even though its response will never reach the end-user.
Requests
The request received by the application is a wrapper for the incoming message. Read more about the default request properties.
Each stack has its own Request
class. You can extend its prototype:
var stack = ;stackRequestprototype { return 'foo';};
To add specific properties to the request, as it's instantiated, you can use
Stack#addRequestInitializer()
:
;
Middleware
Stack#addMiddleware()
can be used to set up a chain of functions that'll be invoked before the
request is passed to the application:
{ return statusCode: 200 html: '<p>Hello world</p>' ;} ;
next
will invoke the next middleware function or indeed the application. It
always returns a promise, albeit rejected if the function throws.
Middleware should not be used to set properties on the request
. Use
request initializers instead.
Plugins
Plugins combine request initializers and middleware in a single object:
;
Low-level error responses
A 503
response is generated 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
, a 504
response is generated. For all other errors a 500
response
is used.
Normally these responses do not have headers or indeed a response body, but they can be customized:
;
Lifecycle events
Events are emitted from the stack when new requests are made, errors occur, or responses are written. Read more about events.
And more!
See API Docs.