Skip to content

Commit

Permalink
add Router.all() method
Browse files Browse the repository at this point in the history
Similar to app.all() but specifically for attaching handlers to all
methods under a standalone router. This is useful for isolating routers
that require "middleware" like features for all routes managed by the
router.
  • Loading branch information
defunctzombie committed Nov 19, 2013
1 parent 89e7264 commit f47c0d9
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
9 changes: 9 additions & 0 deletions lib/router/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,15 @@ Router.prototype.route = function(method, path, callbacks){
return this;
};

Router.prototype.all = function(path) {
var self = this;
var args = [].slice.call(arguments);
methods.forEach(function(method){
self.route.apply(self, [method].concat(args));
});
return this;
};

methods.forEach(function(method){
Router.prototype[method] = function(path){
var args = [method].concat([].slice.call(arguments));
Expand Down
18 changes: 18 additions & 0 deletions test/Router.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
var express = require('../')
, Router = express.Router
, request = require('./support/http')
, methods = require('methods')
, assert = require('assert');

describe('Router', function(){
Expand Down Expand Up @@ -100,4 +101,21 @@ describe('Router', function(){
router.route('get', '/foo', function(){}, function(){});
})
})

describe('.all', function() {
it('should support using .all to capture all http verbs', function() {
var router = new Router();

router.all('/foo', function(){});

var url = '/foo?bar=baz';

methods.forEach(function testMethod(method) {
var route = router.match(method, url);
route.constructor.name.should.equal('Route');
route.method.should.equal(method);
route.path.should.equal('/foo');
});
})
})
})

0 comments on commit f47c0d9

Please sign in to comment.