1.0.14 • Published 9 years ago
promise-expresso v1.0.14
Promise Expresso
Expresso is middleware that makes defining other middelware or route handlers much easier.
Usage
Creating the middleware is easy. Just do:
const expresso = require( "promise-expresso" );
var middleware = expresso();
You can optionally provide a handlers
argument, which is an object of handler functions. Handler functions take the result/error of a resolved/rejected promise and do somethijng with it. A handler function takes the following arguments: (req, res, next, output)
.
The default handler functions are:
jsonResult
wraps the output of the promise like:{ result : output }
jsonError
wraps the output of the promise like:{ error : output }
next
discards the output and callsnext()
(useful for middleware)nextError
passes the output to thenext
fn. (useful for middleware)
Once the middleware is added, you specify promises by passing them as an argument into res.promise
. This function accepts two different sets of arguments:
(nextFn, handlers, promise)
looks up the resolved promise handler by usinghandlers.result
and the rejected promise handler by usijnghandlers.error
(promise)
assumes the handler for resolved promises isjson
and rejected promises isjsonError
.nextFn
is set tonull
as it is not required by either of the handlers
Sending a wrapped JSON response
app.get( "/foo", function( req, res ) {
res.promise( async function() {
return await getSomeResult();
});
});
Adding middleware
app.use( function( req, res, next ) {
res.promise( next, { result : "next", error : "nextError" }, async function() {
res.foobar = await getSomeResult();
});
});