Skip to content

Instantly share code, notes, and snippets.

@shesek
Last active December 26, 2015 10:19
Show Gist options
  • Select an option

  • Save shesek/7135760 to your computer and use it in GitHub Desktop.

Select an option

Save shesek/7135760 to your computer and use it in GitHub Desktop.
Merge multiple connect middlewares into a single middleware that calls them in order
connect_middlewares = (fns...) -> (req, res, out) ->
curr = 0
do next = (err=null) ->
return out err if err? or not fn = fns[curr++]
fn req, res, next
function connect_middlewares() {
var fns = arguments
return function(req, res, out) {
var fn, curr = 0;
(function next(err) {
(err || !(fn = fns[curr++]))
? out(err)
: fn(req, res, next)
})()
}
}
// Example: app.use(connect_middlewares(middleware1, middleware2, middleware3))
// This is the same as using `app.use(connect(middleware1, ...))`, but more lightweight
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment