Added app.VERB() -> [Function...], app.lookup.VERB(), and app.match.VERB(). Closes #606
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 799f790 · Browse files · 2 files·+66 added−0 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+66 added−0 removed
- tests:+42 added−0 removed
- other:+24 added−0 removed
Provenance
- Commit
6dcf6f4- Tree
d9c725a- Author
Tj Holowaychuk <tj@vision-media.ca>- Committer
Tj Holowaychuk <tj@vision-media.ca>- Parent
799f790
2 files changed+66 added−0 removed
modified:lib/http.js+24 added
42 unmodified lines43 */44 45 Server.prototype.init = function(middleware){46 var self = this;47+ this.match = {};48+ this.lookup = {};49 this.settings = {};50 this.redirects = {};51 this.isCallbacks = {};52 this.viewHelpers = {};56 unmodified lines109 110 // register error handlers on "listening"111 // so that they disregard definition position.112 this.on('listening', this.registerErrorHandlers.bind(this));113+114+ // route lookup methods115+ methods.forEach(function(method){116+ self.match[method] = function(url){117+ return self.router.match(url, 'all' == method118+ ? null119+ : method);120+ };121+122+ self.lookup[method] = function(path){123+ return self.router.lookup(path, 'all' == method124+ ? null125+ : method);126+ };127+ });128 };129 130 /**131 * When using the vhost() middleware register error handlers.326 unmodified lines458 function generateMethod(method){459 Server.prototype[method] = function(path, fn){460 var self = this;461 462+ // Lookup463+ if (1 == arguments.length) {464+ return this.router.lookup(path, 'all' == method465+ ? null466+ : method);467+ }468+469 // Ensure router is mounted470 if (!this.__usedRouter) {471 this.use(this.router);472 }28 unmodified linesmodified:test/express.test.js+42 added
559 unmodified lines560 561 assert.response(app,562 { url: '/user/12', method: 'OPTIONS' },563 { headers: { Allow: 'GET,PUT' }});564+ },565+ 566+ 'test app.lookup': function(){567+ var app = express.createServer();568+ app.get('/user', function(){});569+ app.get('/user/:id', function(){});570+ app.get('/user/:id/:op?', function(){});571+ app.put('/user/:id', function(){});572+ app.get('/user/:id/edit', function(){});573+574+ app.get('/user').should.have.length(1);575+ app.get('/user/:id').should.have.length(1);576+ app.get('/user/:id/:op?').should.have.length(1);577+ app.put('/user/:id').should.have.length(1);578+ app.get('/user/:id/edit').should.have.length(1);579+ app.get('/').should.have.be.empty;580+ app.all('/user/:id').should.have.length(2);581+ 582+ app.lookup.get('/user').should.have.length(1);583+ app.lookup.get('/user/:id').should.have.length(1);584+ app.lookup.get('/user/:id/:op?').should.have.length(1);585+ app.lookup.put('/user/:id').should.have.length(1);586+ app.lookup.get('/user/:id/edit').should.have.length(1);587+ app.lookup.get('/').should.have.be.empty;588+ app.lookup.all('/user/:id').should.have.length(2);589+ },590+ 591+ 'test app.match': function(){592+ var app = express.createServer();593+ app.get('/user', function(){});594+ app.get('/user/:id', function(){});595+ app.get('/user/:id/:op?', function(){});596+ app.put('/user/:id', function(){});597+ app.get('/user/:id/edit', function(){});598+599+ app.match.get('/user').should.have.length(1);600+ app.match.get('/user/12').should.have.length(2);601+ app.match.get('/user/12/:op?').should.have.length(1);602+ app.match.put('/user/100').should.have.length(1);603+ app.match.get('/user/5/edit').should.have.length(2);604+ app.match.get('/').should.have.be.empty;605+ app.match.all('/user/123').should.have.length(3);606 }607 };