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

Nested routers behaviour changed in koa@7 #369

Closed
pmrukot opened this issue Aug 8, 2017 · 2 comments · Fixed by #423
Closed

Nested routers behaviour changed in koa@7 #369

pmrukot opened this issue Aug 8, 2017 · 2 comments · Fixed by #423

Comments

@pmrukot
Copy link

pmrukot commented Aug 8, 2017

Hi 👋

I've encountered an issue while migrating to koa@2 with koa-router@7. Im nesting my routes like these:

const router1 = new Router({prefix: '/firstPrefix'});
const router2 = new Router({prefix: '/secondPrefix'});

router2.use(resourceRouterA.routes());
router2.use(resourceRouterB.routes());
router2.use(resourceRouterC.routes());

router1.use(router2.routes());

And I've noticed that the paths in my routes changed, to be specific (.*) was added to them. So the paths looked like below:

Before (koa@5):
/firstPrefix/secondPrefix

After (koa@7):
/firstPrefix(.*)/secondPrefix(.*)

And my routes broke as you may guess. (needed to add additional dummy string to match syntax)

So I went to the sources:

koa-router@5:
https://github.com/alexmingoia/koa-router/blob/5.x/lib/router.js#L221
https://github.com/alexmingoia/koa-router/blob/5.x/lib/router.js#L258

koa-router@7:
https://github.com/alexmingoia/koa-router/blob/master/lib/router.js#L246
https://github.com/alexmingoia/koa-router/blob/master/lib/router.js#L276

In koa-router@5 there is:

var path = '(.*)';
(...)
router.register(path);

while in koa-router@7:

var path;
(...)
router.register(path || '(.*)');

The problem is, that in koa-router@7 path is always truthy, and the prefix of (.*) is being set while no string is specified in first argument:
https://github.com/alexmingoia/koa-router/blob/master/lib/router.js#L257
https://github.com/alexmingoia/koa-router/blob/master/lib/router.js#L265

I've also found some relevant issues here:

#155
Similar to mine, I've changed the routes declaration to:

const router1 = new Router({prefix: '/firstPrefix'});
const router2 = new Router({prefix: '/secondPrefix'});

router2.use('', resourceRouterA.routes());
router2.use('', resourceRouterB.routes());
router2.use('', resourceRouterC.routes());

router1.use('', router2.routes());

And now it seems to work as before. But the discussion in the issue above was just cut, with no explanation or followup if this is a bug or not.

I've also found these ones that may be relevant:
8665620
#260

So the question: is this intentional behaviour? I haven't found anything in docs about this change.

@YanLobat
Copy link

YanLobat commented Sep 19, 2017

@pmrukot Sorry If I misunderstood you, but It seems that #244 is relevant to your issue.

@amangeot
Copy link

amangeot commented Jan 18, 2018

@pmrukot does this solve your issue?

for (let layer of router.stack) {
  layer.path = layer.path.replace(/\(\.\*\)/g, '')
}

On the main router, in order to remove the undesired (.*)

jbielick added a commit that referenced this issue Jan 20, 2018
fixes what looks like a regression in 8665620 in which `path` inside of
`.use()` is defaulted to a wildcard `(.*)` instead of remaining
undefined while the line:

if (path) nestedLayer.setPrefix(path)

runs.

https://github.com/alexmingoia/koa-router/blob/840591d4f660ddde98aedc689dea862a7e63f55b/lib/router.js#L265

This fixes #369 and #410
A test case has been added.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants