Skip to content

Conversation

@schwarmco
Copy link

@schwarmco schwarmco commented May 12, 2020

Fixes #34

Though, the ctx._matchedRoute fails when handling middleware-chains, i'd go with introducing ctx.routerPath instead of replacing it to be consistent with ctx.routerName and to not break backwardscompatibility(?).

To note is, that a Middleware can only know about the "correct" route, after it was hit. This means:

// this does not work
const middleware = async function (ctx, next) {
  expect(ctx.routerPath).to.be('/users/list');
  return next();
};
router.use(middleware);

// this works
const middleware = async function (ctx, next) {
  await next();
  expect(ctx.routerPath).to.be('/users/list');
};
router.use(middleware);

@schwarmco
Copy link
Author

schwarmco commented May 12, 2020

Just found out, there's already a router-option called routerPath. Based on if we want to "break" ctx._matchedRoute i can use either that or come up with a new name for it to avoid confusion.

@schwarmco
Copy link
Author

I renamed it to ctx.matchedRoute now - maybe you can have a look @niftylettuce ?

There are these two things, which probably reflect the same feature, but work different:

router/lib/router.js

Lines 355 to 359 in 6131069

const mostSpecificLayer = matchedLayers[matchedLayers.length - 1]
ctx._matchedRoute = mostSpecificLayer.path;
if (mostSpecificLayer.name) {
ctx._matchedRouteName = mostSpecificLayer.name;
}

ctx.routerName = layer.name;

@kroleg
Copy link

kroleg commented Jun 30, 2020

@schwarmco #34 was just fixed by #93, so i assume this one can be closed

@niftylettuce
Copy link
Contributor

@koa/router v9.1.0 released to npm and mirrored to koa-router as well

https://github.com/koajs/router/releases/tag/v9.1.0

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 this pull request may close these issues.

The ctx. _matchedRoute is not the match route.

3 participants