Skip to content
Myr

Merge branch 'refactor/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 · parents eac6665, 5688ea6 · Browse files · 4 files+21 added−24 removed
This commit

State

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

Diff stats

4 files+21 added−24 removed

  • tests:+17 added−3 removed
  • other:+4 added−21 removed

4 files+21 added−24 removed

modified:lib/http.js+3 added-20 removed
448 unmodified lines
449450// Generate routing methods451452function generateMethod(method){453 Server.prototype[method] = function(path, fn){453 Server.prototype[method] = function(path){454 var self = this;455456 // Lookup457 if (1 == arguments.length) {
2 unmodified lines
460 : method);461 }462463 // Ensure router is mounted464 if (!this.__usedRouter) {465 this.use(this.router);466 }464 if (!this.__usedRouter) this.use(this.router);465468 // Route specific middleware support469 if (arguments.length > 2) {470 var args = Array.prototype.slice.call(arguments, 1);471 fn = args.pop();472 (function stack(middleware){473 middleware.forEach(function(fn){474 if (Array.isArray(fn)) {475 stack(fn);476 } else {477 self[method](path, fn);478 }479 });480 })(args);481 }482466 // Generate the route484 this.routes[method](path, fn);467 this.routes[method].apply(this, arguments);468 return this;469 };470 return arguments.callee;471}
6 unmodified lines
modified:support/connectnot shown

Its text is not shown, modified.

modified:test/express.test.js+17 added-3 removed
433 unmodified lines
434 { body: 'restored' }435 );436 },437 438 'test route middleware': function(){439 var app = express.createServer();438 'test route middleware': function(beforeExit){439 var app = express.createServer()440 , calls = 0;441442 function allow(role) {443 return function(req, res, next) {444 // this is totally not real, dont use this :)
15 unmodified lines
460 }461 }462 }463464 app.param('user', function(req, res, next, user){465 ++calls;466 next();467 });468469 app.get('/xxx', allow('member'), restrictAge(18), function(req, res){470 res.send(200);471 });472
3 unmodified lines
476477 app.get('/tobi', [allow('member')], [[restrictAge(18)]], function(req, res){478 res.send(200);479 });480 481 app.get('/user/:user', [allow('member')], [[restrictAge(18)]], function(req, res){482 res.send(200);483 });484475 ['xxx', 'booze', 'tobi'].forEach(function(thing){485 ['xxx', 'booze', 'tobi', 'user/tj'].forEach(function(thing){486 assert.response(app,487 { url: '/' + thing },488 { body: 'Unauthorized', status: 401 });489 assert.response(app,
2 unmodified lines
492 assert.response(app,493 { url: '/' + thing, headers: { 'X-Role': 'member', 'X-Age': 18 }},494 { body: 'OK', status: 200 });495 });496497 beforeExit(function(){498 calls.should.equal(3);499 });500 },501 502 'test named capture groups': function(){503 var app = express.createServer();
118 unmodified lines
modified:package.json+1 added-1 removed