Skip to content

Commit

Permalink
Inherit param middleware when nesting routers. Fixes ZijianHe#170.
Browse files Browse the repository at this point in the history
  • Loading branch information
alexmingoia authored and Rui Quelhas committed Oct 16, 2015
1 parent bafe278 commit 73c38d4
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 0 deletions.
6 changes: 6 additions & 0 deletions lib/router.js
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,12 @@ Router.prototype.use = function () {
router.stack.push(layer);
});

if (router.params) {
Object.keys(router.params).forEach(function (key) {
fn.router.param(key, router.params[key]);
});
}

return false;
}

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

it('runs parent parameter middleware for subrouter', function (done) {
var app = koa();
var router = new Router();
var subrouter = new Router();
subrouter.get('/:cid', function *(next) {
this.body = {
id: this.params.id,
cid: this.params.cid
};
});
router
.param('id', function *(id, next) {
this.params.id = 'ran';
if (!id) return this.status = 404;
yield next;
})
.use('/:id/children', subrouter.routes());

request(http.createServer(app.use(router.routes()).callback()))
.get('/did-not-run/children/2')
.expect(200)
.end(function(err, res) {
if (err) return done(err);
res.should.have.property('body');
res.body.should.have.property('id', 'ran');
res.body.should.have.property('cid', '2');
done();
});
});
});

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

0 comments on commit 73c38d4

Please sign in to comment.