Skip to content

Commit

Permalink
Fix router.use to accept array of middleware without path
Browse files Browse the repository at this point in the history
  • Loading branch information
dougwilson committed Sep 18, 2014
1 parent bf1980f commit 7289171
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 2 deletions.
1 change: 1 addition & 0 deletions History.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ unreleased
==========

* Fix regression for empty string `path` in `app.use`
* Fix `router.use` to accept array of middleware without path
* Improve error message for bad `app.use` arguments

4.9.1 / 2014-09-16
Expand Down
14 changes: 12 additions & 2 deletions lib/router/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -412,9 +412,19 @@ proto.use = function use(fn) {
var self = this;

// default path to '/'
// disambiguate router.use([fn])
if (typeof fn !== 'function') {
offset = 1;
path = fn;
var arg = fn;

while (Array.isArray(arg) && arg.length !== 0) {
arg = arg[0];
}

// first arg is the path
if (typeof arg !== 'function') {
offset = 1;
path = fn;
}
}

var callbacks = utils.flatten(slice.call(arguments, offset));
Expand Down
22 changes: 22 additions & 0 deletions test/Router.js
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,28 @@ describe('Router', function(){
router.use.bind(router, '/', null).should.throw(/requires middleware function.*Null/)
router.use.bind(router, '/', new Date()).should.throw(/requires middleware function.*Date/)
})

it('should accept array of middleware', function(done){
var count = 0;
var router = new Router();

function fn1(req, res, next){
assert.equal(++count, 1);
next();
}

function fn2(req, res, next){
assert.equal(++count, 2);
next();
}

router.use([fn1, fn2], function(req, res){
assert.equal(++count, 3);
done();
});

router.handle({ url: '/foo', method: 'GET' }, {}, function(){});
})
})

describe('.param', function() {
Expand Down

0 comments on commit 7289171

Please sign in to comment.