Skip to content
Myr

Recursively apply route middleware

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by Tj Holowaychuk · committed by Tj Holowaychuk · parent db95481 · Browse files · 2 files+13 added−10 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

2 files+13 added−10 removed

  • tests:+1 added−1 removed
  • other:+12 added−9 removed

2 files+13 added−10 removed

modified:lib/express/server.js+12 added-9 removed
305 unmodified lines
306// Generate routing methods307308(function(method){309 Server.prototype[method] = function(path, fn){310 var self = this;311312 // Ensure router is mounted313 if (!this.__usedRouter) {314 this.use(this.router);315 }316317 // Route specific middleware support318 if (arguments.length > 2) {317 for (var i = 1, len = arguments.length - 1; i < len; ++i) {318 if (Array.isArray(arguments[i])) {319 for (var j = 0, jlen = arguments[i].length; j < jlen; ++j) {320 this[method](path, arguments[i][j]);319 var args = Array.prototype.slice.call(arguments, 1);320 fn = args.pop();321 (function stack(middleware){322 middleware.forEach(function(fn){323 if (Array.isArray(fn)) {324 stack(fn);325 } else {326 self[method](path, fn);327 }322 } else {323 this[method](path, arguments[i]);324 }325 }326 fn = arguments[arguments.length - 1];328 });329 })(args);330 }331332 // Generate the route333 this.routes[method](path, fn);
4 unmodified lines
modified:test/express.test.js+1 added-1 removed
354 unmodified lines
355 app.get('/booze', [allow('member')], restrictAge(18), function(req, res){356 res.send(200);357 });358359 app.get('/tobi', [allow('member')], [restrictAge(18)], function(req, res){359 app.get('/tobi', [allow('member')], [[restrictAge(18)]], function(req, res){360 res.send(200);361 });362363 ['xxx', 'booze', 'tobi'].forEach(function(thing){
12 unmodified lines