Skip to content

lgvo/express-promise-wrapper

Repository files navigation

Express Promise Wrapper

Build Status Coverage Status npm version Code Climate npm

A simple wrapper to to transform promise results into express middleware style (req, res, next) functions.

Installation

$ npm install --save express-promise-wrapper

Usage

Express app

var withPromise = require('express-promise-wrapper'),
    express = require('express');


var expressApp = express();

// function that return a promise
function promised(body) {
    return ...; // promise
}

express.use(withPromise.wrap(promised));

This will create a wrap function that calls "next" callback if catch a error and use objects returns to write to the response.

The return object

You should return a object that have a write function that receives a Express.Response. Example:

function promise(id) {
    return somePromise.then(function(result) {
        return {
            write: function(res) {
                res.send('text');
            }
        };
    })
}

The project comes with helpers to create some of this objects:

var json = withPromise.json,
    jsonCollection = withPromise.jsonCollection,
    created = withPromise.created;

json(obj);
// equivalent
res.json(obj);

jsonCollection(arr);
// equivalent
res.json({size: arr.length, data: arr});

created(location, id);
// equivalent
res.location(location);
res.send(id, 201);

See Also

  • restful-express declarative way to define Express routers using decorators.
  • di-decorators easy to use, little dependency injection framework on top of decorators

Dependencies

Contributing

  • Please take the time to star the project if you like it! "npm star express-promise-wrapper" and also on github express-promise-wrapper.
  • Feel free to fork, and if you are planning to add more features please open a issue so we can discuss about.

License

MIT

About

Wrapper to use express with promises

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published