express-modern
TypeScript icon, indicating that this package has built-in type declarations

1.0.0 • Public • Published

express-modern

use async function or generator function with express

Build Status Coverage Status npm version npm downloads npm license

Install

npm i express-modern --save

supported case

  • normal handler app.get('/', modern((req, res, next) => { ... }))
  • error handler app.get('/', modern((err, req, res, next) => { ... }))
  • param handler app.param('user', modern((req, res, next, user) => { ... }))

API

const modern = require('express-modern')

generator function

const app = express();
app.get('/', modern(function * (req, res, next) {
  yield new Promise(r => setTimeout(r, 10));
  res.send('generator function');
}));

const res = yield request(app)
  .get('/')
  .endAsync();
res.text.should.match(/generator/);

the generator function will be wrap via co.wrap. and the modern function will take care of the arity of the handler. see http://expressjs.com/en/guide/error-handling.html

async function

const app = express();
app.get('/', modern(async function(req, res, next) {
  await new Promise(r => setTimeout(r, 10));
  res.send('async function');
}));

const res = yield request(app)
  .get('/')
  .endAsync();
res.text.should.match(/async/);

the async function will be called & the rejected value will be passed to next automatically

normal function

const app = express();
app.get('/', modern((req, res, next) => {
  res.send('normal function');
}));

const res = yield request(app)
  .get('/')
  .endAsync();

res.text.should.match(/normal/);

the modern function does nothing on normal function.

Changelog

CHANGELOG.md

License

the MIT License http://magicdawn.mit-license.org

Package Sidebar

Install

npm i express-modern

Weekly Downloads

11

Version

1.0.0

License

MIT

Unpacked Size

6.21 kB

Total Files

5

Last publish

Collaborators

  • magicdawn