Respire: Expressive, Succinct Promise Interfaces for Routing in Express
Express is often used to aggregate data from APIs and databases into views. Promises make control flow and error handling clearer. This express middleware adds some APIs to the request and response objects to simplify the typical fetch/render/respond use case.
Installation
npm install --save respire
Usage
The middleware adds some promise-friendly sugar to req and res.
var respire = ;app;
A promise chain helps to organise the route function into steps.
app;
req.process
req.process
simply wraps a function call using Q.fcall(fn, req, res)
.
res.renderInto
res.renderInto
uses res.render
under the hood, so templates work as they ever did.
The input to res.renderInto
is the usual map of template fields, except they can all be promises.
The map itself can be promised too, if needs be. So getUserData
might look like this:
{ var userDetails; var username = reqparamsusername; return username: username userDetails: userDetails = userFollowing: userDetails userFeed: userDetails ;}
The output of res.renderInto
is a RenderedContext
. Its .toString()
method returns the rendered template. It also exposes reasons for failed fields on .rejected
, and the original data on .data
. This is great for debugging, and can also be useful in templates when composing views.
res.respond
res.respond
provides withHTML
, withJSON
, withErrorPage
and withErrorJSON
.
Error handling
I/O can be unreliable. Some types of failure might not be recoverable. For example, if the userDetails
promise rejects, the user profile can't be rendered. To throw a tantrum if mandatory fields are missing, respire.demand
can be used. It's backed by object.demand
from the excellent q-combinators library by Beamly.
app;
Problems of this nature usually call for an error page. res.respond.withErrorPage
will serve one.
- It expects
err.statusCode
to be an HTTP error code (e.g.401
) - It expects a template to exist for that code in
<views>/errors
(e.g.401.hbs
or, failing that,4xx.hbs
)
respire.middleware.errorPages
The error page middleware ensures that all types of unhandled error on all routes result in error pages being served. It also provides some options.
app;
The ?debug
feature gives you access to a stack trace for the error. If the error represents multiple failed required fields, they will all be listed.
Composing views
It's common to render pages from multiple views. A template field can be a promise for another RenderedContext:
{ var userDetails; var username = reqparamsusername; return username: username userDetails: userDetails = renderedFollowing: userDetails renderedFeed: userDetails ;}
It's also possible to render the output of one view straight into another:
app;
Here's chrome/standard-page.hbs
:
{{this.data.userDetails.displayName}} {{{this}}}
Notice how (in handlebars, at least) accessing {{{this}}}
calls toString
on the RenderedContext, while the original data is still accessible on this.data
.
Future plans
Next up:-
- Shorthand for render + respond (as you expect from res.render)
- Debugging rendered pages (i.e to see which promises rejected and why)
- Grouping other logging activity by the pageView which triggered it
- A good pattern for setting headers, serving redirects, etc.