I need to recover the original route at the time of a request. Either the original one with the :Variable strings, or the regular expression. Basically I want to have some key that groups all requests that hit the same route in a generic, high-level pre-processor.
The problem is that my routes are fairly complex with nested apps and routers, so app.routes.map doesn’t contain the routes.
Is there a way to reverse engineer the route from the request object, or is there a place where I can get a global list of routes that I could match and search agaisnt the current request?
Problem courtesy of: Killroy
Solution
The specific route should be available in req.route
( docs
). That contains several properties, including req.route.regexp
(the compiled regular expression for the route), and req.route.params
(array of matched parameters).
EDIT:The route isn’t going to be attached until the request is dispatched.
It sounds like you’re trying to predict the route before it’s dispatched. In that case, using app.routes
might normally work. The problem is that you have sub-apps with their own routers. app.routes
only contains routes for the top-level app
and not for any sub-apps.
The answer is: There is no global view of routes.
Your sub-apps look exactly like middleware to the parent app. It does not know or care that there is routing going on down there.
If you need to do this, you have to do it manually, e.g., create a global array where you register your sub-apps.
Solution courtesy of: Nate
Discussion
There is currently no discussion for this recipe.
This recipe can be found in it’s original form on Stack Over Flow
.
CoLaBug.com遵循[CC BY-SA 4.0]分享并保持客观立场,本站不承担此类作品侵权行为的直接责任及连带责任。您有版权、意见、投诉等问题,请通过[eMail]联系我们处理,如需商业授权请联系原作者/原网站。