Skip to content

Commit

Permalink
Run nested router middleware after parent's. Fixes #156.
Browse files Browse the repository at this point in the history
  • Loading branch information
alexmingoia committed Sep 26, 2015
1 parent 0e2188a commit 7b1b917
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/router.js
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,7 @@ Router.prototype.use = function (path, middleware) {
if (router.opts.prefix) {
layer.setPrefix(router.opts.prefix);
}
router.stack.push(layer);
router.stack.unshift(layer);
});

return false;
Expand Down
34 changes: 34 additions & 0 deletions test/lib/router.js
Original file line number Diff line number Diff line change
Expand Up @@ -386,6 +386,40 @@ describe('Router', function() {
done();
});
});

it('runs router middleware before subrouter middleware', function (done) {
var app = koa();
var router = new Router();
var subrouter = new Router();

router.use(function *(next) {
this.foo = 'boo';
yield next;
});

subrouter
.use(function *(next) {
this.foo = 'foo';
yield next;
})
.get('/bar', function *(next) {
this.body = {
foobar: this.foo + 'bar'
};
});

router.use('/foo', subrouter.routes());
app.use(router.routes());
request(http.createServer(app.callback()))
.get('/foo/bar')
.expect(200)
.end(function (err, res) {
if (err) return done(err);

expect(res.body).to.have.property('foobar', 'foobar');
done();
});
});
});

describe('Router#register()', function() {
Expand Down

0 comments on commit 7b1b917

Please sign in to comment.