This package has been deprecated

Author message:

Package no longer supported. Contact Support at https://www.npmjs.com/support for more info.

promised-router
TypeScript icon, indicating that this package has built-in type declarations

2.3.0 • Public • Published

promised-router

NPM Version License Build Status

Express Router with promises.

Allows you to define express routes that return promises instead of calling next().

npm install --save promised-router

If the promise resolves successfully, then it is sent to res.json(result).

Otherwise, if the promise rejects, then the error reason is passed to next(err).

The router object can be passed to an ordinary express.use('/path', router) call.

It also supports .param() and .use(), with the only other difference being that error handlers are now added using .error() instead of .use(). This was done because it depends on fn.length which can be counter-intuitive sometimes.

To simulate next('route'), simply throw 'route'.

Usage

const Router = require('promised-router');
 
module.exports = new Router()
.param('userId', req => {
  return User.findById(req.params.userId)
  .then(user => req.user = user);
})
.use(function (req, res) {
  return Session.findById(req.query.access_token)
  .then(session => req.session = session);
})
.get('/example/:id', function (req, res) {
  return User.findById(req.params.id);
})
.use('/other', require('./other_routes'))
.error(function (err, req, res) {
  res.status(400);
  return {message: err.message};
})

... translates to ...

const Router = require('express').Router;
 
module.exports = new Router()
.param('userId', function (req, res, next) {
  User.findById(req.params.userId)
  .then(function (user) {
    req.user = user;
    next();
  }, next);
})
.use(function (req, res, next) {
  Session.findById(req.query.access_token)
  .then(session => req.session = session)
  .then(function () {
    next();
  }, next);
})
.get('/example/:id', function (req, res, next) {
  return User.findById(req.params.id)
  .then(function (user) {
    res.json(user);
  }, next);
})
.use('/other', require('./other_routes'))
.use(function (err, req, res, next) { //eslint-disable-line
  res.status(400);
  res.json({message: err.message});
})

Readme

Keywords

Package Sidebar

Install

npm i promised-router

Weekly Downloads

2

Version

2.3.0

License

MIT

Unpacked Size

10.6 kB

Total Files

7

Last publish

Collaborators

  • aantthony