A simple wrapper to to transform promise results into express middleware style (req, res, next) functions.
$ npm install --save express-promise-wrapper
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.
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);
- restful-express declarative way to define Express routers using decorators.
- di-decorators easy to use, little dependency injection framework on top of decorators
- Express Arguments resolver used to resolve the arguments names.
- 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.