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

The ctx. _matchedRoute is not the route I requested! #516

Closed
BainDragneel opened this issue Oct 23, 2019 · 6 comments · May be fixed by #524 or #525
Closed

The ctx. _matchedRoute is not the route I requested! #516

BainDragneel opened this issue Oct 23, 2019 · 6 comments · May be fixed by #524 or #525

Comments

@BainDragneel
Copy link

node.js version: 12.12.0

npm/yarn and version: 6.11.3

koa-router version: 7.4.0

koa version: 2.10.0

hi,I have some problems. I use koa tag 2.7.0 .
Now I have two routes, one is "/api/order/list" and another is "/api/order/:orderId" .
I want to log each request. When I request the second interface with params, I want the url I am recording to be the url of my own defined api, not the url directly with params.For example, when I request "http://127.0.0.1:3000/api/order/57d52c52403f2ee865738ec7", I want to record that the url is "/api/order/:orderId" instead of "/api/order/57d52c52403f2ee865738ec7".
But ctx.url ctx. originalUrl is not what I want, and ctx. _matchedRoute is what I want.However, I have a new problem. When I request "/api/order/list", the value of ctx._matchedRoute is still "apr/order/:orderId" instead of "/api/order/list" and ctx.matched is an array of two object elements.
Why the _matchedRoute is not the route I requested?

Code sample:

http:127.0.0.1:3000/api/order/list
image
image

http:127.0.0.1:3000/api/order/:orderId
image
image

@jiladahe1997
Copy link

I have found something that may help.

node_modules\koa-router\lib\router.js

 335 var mostSpecificLayer = matchedLayers[matchedLayers.length - 1];
 336 ctx._matchedRoute = mostSpecificLayer.path;

As shown in the code, ctx._matcheRoute is the most matched route path insteadof requested.

schwarmco added a commit to schwarmco/koa-router that referenced this issue May 12, 2020
Fixes ZijianHe#516 

To not break backwardscompatibility, i'd not "fix" the `_matchedRoute` but introduce `ctx.routerPath` analog to `ctx.routerName`
@schwarmco
Copy link

schwarmco commented May 12, 2020

I ran into the same problem. The issue is, that matchedLayers contains - as the name suggests - all matched layers, that will be put in the middleware-chain (because the regex matches).

In the case above, it's because :orderId could also be list. So, if the orderId-Middleware decides, it doesn't want to handle the request, it can call next() and the list-route will be called. That way the "mostSpecificLayer" isn't necessarily the "last" or "current" one.

With that being said, the ctx._matchedRoute is probably a bug - or at least misleadingly named.

I've created PR #524, which would populate ctx.routerPath

@tuananh
Copy link

tuananh commented May 13, 2020

@schwarmco this is the official one from koa org https://github.com/koajs/router , a fork of this repo

@schwarmco
Copy link

@tuananh thanks - i opened a pull-request there... just landed here, because i searched for this exact same issue :)

@BainDragneel
Copy link
Author

Thanks very much, I have some things I did not pay attention to this issue in time, sorry!

@BainDragneel
Copy link
Author

Already got help:https://github.com/koajs/router/releases/tag/v9.1.0 Thx!!!!!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
4 participants