Skip to content

Instantly share code, notes, and snippets.

@spion
Created August 27, 2012 12:17
Show Gist options
  • Save spion/3487954 to your computer and use it in GitHub Desktop.
Save spion/3487954 to your computer and use it in GitHub Desktop.
Create a middleware from a list of middlewares.
module.exports = function () {
var list = arguments;
return function (req, res, realNext) {
var id = -1;
var innerNext = function (err) {
if (err) return realNext(err);
var nextFn = ++id < list.length - 1 ? innerNext : realNext,
mw = list[id];
mw(req, res, nextFn);
};
innerNext();
};
};
// usage example:
// var middlewarelist = require('connect-list');
// var passportWithDeps = middlewarelist(express.bodyParser(), express.methodOverride(), mySession, passport.initialize(), passport.session());
// somewhere other
// app.use('/login', passportWithDeps); // it makes sure they're loaded in the correct order.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment