tinyhttp: req.route always returns undefined
Describe the bug
req.route always returns undefined
https://tinyhttp.v1rtl.site/docs#reqroute
To Reproduce
Steps to reproduce the behavior:
- Just follow docs as explained here.
- Add the endpoint from docs and display
req.route - Its always undefined.
Expected behavior
The output should be similar to something written in docs.
{
path: '/user/:id?',
method: 'GET',
handler: [Function: userIdHandler],
type: 'route'
}
Versions
node: v16.20.0@tinyhttp/app: 2.0.33
About this issue
- Original URL
- State: closed
- Created 9 months ago
- Comments: 19 (11 by maintainers)
Hey @atif-saddique-deel the implementation is correct and I’ll explain why but first, change the
subRoute.getto the followingNotice that the
route.pathis nowuserswhich means it is assigned correctly. Your code was still showing the correct route but 2 of the paths were identical.Now coming to the why, when mounting an app, we don’t prepend each path with the mount point. Rather, we let the subapp handle the routing. Essentially, the path you see is the path in the subapp not the full path. The full path, with the subapp mount point prepended, is
req.urlwhich is essentially the URL itself. This can be seen inhttps://github.com/tinyhttp/tinyhttp/blob/65cd2f12520ec62186d5bb4511af7b5daba3b0f2/packages/app/src/app.ts#L216-L232
hope this helps! let me know if you have any more questions.
A minor version will be released once the pr is merged but you can build my branch and create a new file at the root which references
Appfrom./packages/app/src/app.ts. Like thisTo run this, run the following command
To build, here’s a guide https://github.com/aarontravass/tinyhttp/blob/aaron/route/CONTRIBUTING.md#local-setup
true, there should be a notice that it can be enabled with
enableReqRoute. Docs need a fix.@atif-saddique-deel I pushed a fix to https://github.com/aarontravass/tinyhttp/tree/aaron/route
Can you try it out once?
Thanks for raising this issue.
You need to enable req route by setting
enableReqRouteto true in settings. See below