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

Get the pattern for the current route #500

Open
rightaway opened this issue Mar 28, 2019 · 4 comments
Open

Get the pattern for the current route #500

rightaway opened this issue Mar 28, 2019 · 4 comments

Comments

@rightaway
Copy link

If I do ctx.url I can get the actual URL like /users/123/456, but I'd like to suggest a feature where we can get the router pattern that was matched like /users/:idA/:idB for the current route. Something like ctx.router.pattern.

@DinaYakovlev
Copy link

you can get the matched route by using: ctx._matchedRoute

@ChrisLahaye
Copy link

@DY6229 do you know how to get the matched route when using nested routers? In that case the info dissapears

@DinaYakovlev
Copy link

DinaYakovlev commented Jul 14, 2019

@ChrisLahaye Yes I encountered the same issue. couldn't find any simple solution to this, except for getting the route by-myself using the router.match() function.
for example lets say you have the url: /pets/{pet_id}/test
you can see that ctx._matchedRoute will look something like this: /pets/{pet_id}(.*)
the /pets/{pet_id} is your main route, so you can conclude that /test will be the sub route.
using router.match('/test', method) you can get all possible routes and from there to build your own matched route.
You can look at my full implementation here:
https://github.com/Zooz/prometheus-api-metrics/blob/07c0c2a364ab1978a86198630464f1a58a905d15/src/koa-middleware.js#L33

@ChrisLahaye
Copy link

@DinaYakovlev thank you for your reply. I just stopped using nested routes and attached multiple routers to my webserver.

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

No branches or pull requests

3 participants