1.0.14 • Published 9 years ago

promise-expresso v1.0.14

Weekly downloads
2
License
MIT
Repository
github
Last release
9 years ago

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:

  1. jsonResult wraps the output of the promise like: { result : output }
  2. jsonError wraps the output of the promise like: { error : output }
  3. next discards the output and calls next() (useful for middleware)
  4. nextError passes the output to the next 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:

  1. (nextFn, handlers, promise) looks up the resolved promise handler by using handlers.result and the rejected promise handler by usijng handlers.error
  2. (promise) assumes the handler for resolved promises is json and rejected promises is jsonError. nextFn is set to null 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();
    });
});
1.0.16

9 years ago

1.0.14

9 years ago

1.0.13

9 years ago

1.0.12

9 years ago

1.0.11

9 years ago

1.0.10

9 years ago

1.0.9

9 years ago

1.0.8

9 years ago

1.0.7

9 years ago

1.0.6

9 years ago

1.0.4

9 years ago

1.0.3

9 years ago

1.0.2

9 years ago

1.0.1

9 years ago

1.0.0

9 years ago