Skip to content

Folders and files

NameName
Last commit message
Last commit date

Latest commit

History

49 Commits

Repository files navigation

conext

use co to write express middleware

install

npm i --save conext

source code (kind of), check it yourself

'use strict';varbluebird=require('bluebird');module.exports=function(gn){varwrapped=bluebird.coroutine(gn);returngn.length>=4 ? function(err,req,res,next){wrapped.apply(this,arguments).catch(next);} : function(req,res,next){wrapped.apply(this,arguments).catch(next);};}

usage

in 3.x, conext will help you call next() if gn.length === 1 or gn.length === 2, so you can avoid next() to be called twice

varconext=require('conext');app.use(conext(function*(req,res){res.locals.resultA=yieldasyncA();// you can omit `return 'next';` here, in conext it will compare returned value to undefined or 'next' and then call next()});app.use(conext(function*(req,res){if(yieldasyncB()){return'next';// or just `return;`, but `return 'next';` is more readable}else{return'next route';// will call you next('route'), which is a express@^4 feature I never use;}},function(req,res,next){res.locals.resultB=true// this will never reached if asyncB() resolved by falsy value});app.use(conext(function*(req,res){res.json(res.locals);returnfalse;// `return false` to indicate you've already taken care of response, like this.respond = true in koa});// ...

If you are using CoffeeScript, it's more convenience for you to keep using next argument because you can use yield whenever you want

conext=require('conext')
app.use conext (req, res, next) ->res.locals.withOutYieldCompilesTo='normal function';
next()
app.use conext (req, res, next) ->res.locals.withYieldCompilesTo=yieldPromise.resolve('generator function');
next()

license

MIT

About

use co to write express middleware

Resources

Stars

0 stars

Watchers

1 watching

Forks

Releases

Packages

Used by

Contributors

Languages