Last active
December 26, 2015 10:19
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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