Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix problems caused by routerPath #144

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions lib/router.js
Original file line number Diff line number Diff line change
Expand Up @@ -377,6 +377,12 @@ Router.prototype.routes = Router.prototype.middleware = function () {
return compose(layerChain)(ctx, next);
};

function restoreRouterPathAfterLayerMatch(ctx, next) {
ctx.routerPath = undefined
return next()
}

dispatch = compose([dispatch, restoreRouterPathAfterLayerMatch])
dispatch.router = this;

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

it('restore a `routerPath` value after layers match', function (done) {
const app = new Koa();
const router1 = new Router();
const router2 = new Router();

router1.get('/users/:userId', function (ctx, next) {
expect(ctx.routerPath).to.be('/users/:userId')
next()
});
router2.get('/users/:id', function (ctx) {
expect(ctx.routerPath).to.be('/users/:id')
ctx.body = 'hello, user is ' + ctx.params.id;
});

app.use(router1.routes());
app.use(router2.routes());
request(http.createServer(app.callback()))
.get('/users/123')
.expect(200)
.end(function (err, res) {
if (err) return done(err);
res.text.should.equal('hello, user is 123');
done();
});
});

it('places a `_matchedRoute` value on the context for current route', function (done) {
const app = new Koa();
const router = new Router();
Expand Down