You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
@dougwilson I see your concerns, and that we have to re-think it, to ensure all methods will be handled correctly.
Another approach could be to simply expose an array of routes in the order they were matched, and if you are going to use the data, you'll have to deal with the different data types.
Assume that you'd replaced my route in server.js with one of you version, then req.originalRoute === [['/api', '/api/v1'], '/user', '/:id']; or req.originalRoute === [/^\/api(?:\/v1)?\//i, '/user', '/:id']. In the "array" version, we might actually be able to identify which of the two was used for the request, and then show that specific one instead of an array - and then we only need to see if we can do something better on RegExp-route.
That's fine. I don't mind adding something for this. We just want to make sure that what gets added to Express core is compatible with all of Express's features, rather than only the features a particular user just happens to be using it is all :)
I'm all in on that - I did not just want this to be a feature for me :) This issue was to rise a discussion on a good way to implement something like this, as the new Router in Express 4 is giving the power to split and nest routes, but the downside is, that it can be hard to know exactly where you are, and for logging purpose it is often useful to be able to group by route rather than exact URL, to know how many request of a specific type has happend.
Which way do you prefer? Some way to concat the routes or should we go with the array of routes a particular request has travelled from entry to the final route?
An array is fine with me... if a use case needs another format, it simply has to handle the transformation from the array to something else, eg a concat-string.
Hey, I can confirm that it would be great to have originalRoute. I'm trying to implement declarative ACL based on routing so it's very uncomfortable to deal with it without knowing what route was matched =(
Since the path parameter of middleware and router handlers can be more than simple strings - examples, this kind of feature will have a hard time being universally useful.
Let's look at some scenarios.
The API should return an array of matched layers/routes.
Consider the resulting array, when a request is made to /secret:
// there are two built-in middleware in express already
app.use(...) // logger
app.use(...) // body parse
app.use(...) // cookie parser
app.use(...) // some more root-level middleware
app.use([['restricted', 'hidden'], /lol/, 'ha*'], (req, res, next) => next())
app.use('/secret', (req, res, next) => next())
app.use('/secret', (req, res, next) => res.send('ABRACADABRA!'))
Questions:
a. Would the resulting array of matched layers/routes be useful?
b. What advantage would it have over manually looking through the code?
The API should return the absolute path of the route (full route)
What should it return when a request is made to /whatever/pineapple or /whatever/apple, in this case?
The API will return /apple, but there is no context about the route handler, unlike req.route.
My suggestion would be to let the users implement their own ways of retrieving the absolute path of the matched route(s), for use cases unique to their own requirements.
@hacksparrow your scenarios are valid, but it seems like you assume that the API should return a combined string. Or am I getting you wrong?
I believe it has already been discussed a lot, that the only solution that would make sense, is to return an array of the actual route definitions, that be a string, regex, an array or whatever is allowed by express.
As I see it, @evanshortiss has actually handled that, though more test scenarios should be created, to ensure the API is handling all kinds of route definitions.
@danieljuhl I am not making any assumptions about what the API should return. In fact, I cannot think of anything that should be returned, which would make sense universally.
Scenarios 1 and 3 highlight the issue of using an array as implemented by @evanshortiss.
I think populating with req.route makes sense, but this example (app.use) is definitely a valid gap, since you do actually respond in the final handler but it (req.matchedRoutes) would yield no matches in this case if we were to use req.route. However, I would argue that you should use app.all('/secrets', handler) or app.METHOD('/secrets', handler) for the last scenario since this would populate req.route and is more sensible from a semantic standpoint. For middleware such as body parser, nothing should be added to the matched routes, since they aren't routes, but are a passthrough.
a. Yes, one would expect this is populating the Array using req.route and app.all: [{path: '/secret', /* other req.route props */}], or the current PR [ '/', '/', '/', '/secret', '/secret' ].
b. This information can be useful at runtime for logging.
True, but it's a passthrough middleware. The purpose of this issue is purely to get the matched routes, not the state of the request object.
Correct, but again these are passthroughs/middleware. One can delete known passthroughs to easily construct the absolute URL, or use them if their scenario requires.
Ultimately the goal of this issue is to enable a developer to programatically determine matched routes which seems like a reasonable request. But I agree, we can see it's a challenge to achieve in a manner that works "universally" and raises the question of can and/or should express attempt provide this.
@evanshortiss yes, I agree it is a reasonable request. It's just that the API is not very convincing or a lot of complexities will be involved (eg: detecting passthroughs). It would be great to have a really elegant solution.
👍 for "This information can be useful at runtime for logging", hadn't thought about it.
@hacksparrow I think the purpose of this PR is to get the routes matched not the 'route handlers' matched. The purpose of this PR, IMHO, is to get the 'context', and not just the final route matched. I do aknowledge that it can look messy if you have a very complex route structure, but I can't see how you can make something messy, look elegant - at least not for now :)
Gotcha. I was thinking that, @danieljuhl , but all the examples in the #3100 seem to indicate the intention of the PR is to build the final route matched, which seems to conflict what your description of your PR. Perhaps I'm just misunderstanding here?
@dougwilson@danieljuhl correct, it's to get context. For me personally I'd like to construct a string that represents the matched URL. @hacksparrow has a nice solution here that would allow one to do what my PR does, without technically adding it as a feature to express.
Another use case would be when supporting Prometheus metrics for all endpoints. To do that, one could use middleware functions at the app-level and use the originalRoute as value of one of the label, something like this:
Since req.url could have user-id, tokens, etc, in it, we cannot use it as the values as that way the number of time-series would be too many.. Not good. So I've kinda this workaround:
.. where normalize is a function which finds & replaces variable-values such as uuid, tokens, etc with something constant like - and just empty-string. It pointlessly consumes some CPU time. We could avoid paying for this if we have originalRoute, or originalPath (whichever sounds good).
Reacted by Cameron Little, Manan Vaghasiya, manvydasu and Sarfaraz Nawaz
The router has been moved, and this would be a feature of the router. You can see what IMO is the best option for this on the Router repo: pillarjs/router#34
Any future conversation should be had over on that thread, as it is where we would make this kind of change.
Activity
dougwilson commentedon Jan 16, 2015
Can you show a little express app that makes use of this as well, please :)?
danieljuhl commentedon Jan 16, 2015
@dougwilson Is the following good enough?
A request could be
GET /api/user/123, and I would expect to be able to get the full route/api/user/:idin the request object.server.jsapi/index.jsapi/user.jsapi/items.jsdougwilson commentedon Jan 16, 2015
How would it work for RegExp routes or array routes?
dougwilson commentedon Jan 16, 2015
i.e. we need it to work if your main file has
or
danieljuhl commentedon Jan 17, 2015
@dougwilson I see your concerns, and that we have to re-think it, to ensure all methods will be handled correctly.
Another approach could be to simply expose an array of routes in the order they were matched, and if you are going to use the data, you'll have to deal with the different data types.
Assume that you'd replaced my route in
server.jswith one of you version, thenreq.originalRoute === [['/api', '/api/v1'], '/user', '/:id'];orreq.originalRoute === [/^\/api(?:\/v1)?\//i, '/user', '/:id']. In the "array" version, we might actually be able to identify which of the two was used for the request, and then show that specific one instead of an array - and then we only need to see if we can do something better on RegExp-route.I don't know if newrelic is able to handle all cases (haven't investigated their code further), but they do something similar to my hacky workaround. https://github.com/newrelic/node-newrelic/blob/master/lib/instrumentation/express.js#L184
dougwilson commentedon Jan 18, 2015
That's fine. I don't mind adding something for this. We just want to make sure that what gets added to Express core is compatible with all of Express's features, rather than only the features a particular user just happens to be using it is all :)
danieljuhl commentedon Jan 18, 2015
I'm all in on that - I did not just want this to be a feature for me :) This issue was to rise a discussion on a good way to implement something like this, as the new Router in Express 4 is giving the power to split and nest routes, but the downside is, that it can be hard to know exactly where you are, and for logging purpose it is often useful to be able to group by route rather than exact URL, to know how many request of a specific type has happend.
Which way do you prefer? Some way to concat the routes or should we go with the array of routes a particular request has travelled from entry to the final route?
dougwilson commentedon Feb 1, 2015
An array that gets built up sounds like the simplest method to me.
danieljuhl commentedon Feb 1, 2015
An array is fine with me... if a use case needs another format, it simply has to handle the transformation from the array to something else, eg a concat-string.
devlato commentedon May 20, 2015
Hey, I can confirm that it would be great to have originalRoute. I'm trying to implement declarative ACL based on routing so it's very uncomfortable to deal with it without knowing what route was matched =(
dougwilson commentedon Jun 19, 2015
Any traction on a PR?
ajfranzoia commentedon Jan 24, 2016
@dougwilson Created a PR about this (#2864).
devlato commentedon Jan 25, 2016
@ajfranzoia Thank you very much
5 remaining items
hacksparrow commentedon Oct 19, 2016
WRT #3100 as well.
Since the
pathparameter of middleware and router handlers can be more than simple strings - examples, this kind of feature will have a hard time being universally useful.Let's look at some scenarios.
The API should return an array of matched layers/routes.
Consider the resulting array, when a request is made to
/secret:Questions:
a. Would the resulting array of matched layers/routes be useful?
b. What advantage would it have over manually looking through the code?
The API should return the absolute path of the route (full route)
What should it return when a request is made to
/whatever/pineappleor/whatever/apple, in this case?Simply returning
/whatever/pineappleor/whatever/applehides the significance of whatrouter.get([['cat', 'dog'], '*apple']does toreq.Best case scenario: the "path" is a simple string
The API will return
/apple, but there is no context about the route handler, unlikereq.route.My suggestion would be to let the users implement their own ways of retrieving the absolute path of the matched route(s), for use cases unique to their own requirements.
@dougwilson @ajfranzoia @danieljuhl @devlato @evanshortiss
danieljuhl commentedon Oct 19, 2016
@hacksparrow your scenarios are valid, but it seems like you assume that the API should return a combined string. Or am I getting you wrong?
I believe it has already been discussed a lot, that the only solution that would make sense, is to return an array of the actual route definitions, that be a string, regex, an array or whatever is allowed by express.
As I see it, @evanshortiss has actually handled that, though more test scenarios should be created, to ensure the API is handling all kinds of route definitions.
hacksparrow commentedon Oct 19, 2016
@danieljuhl I am not making any assumptions about what the API should return. In fact, I cannot think of anything that should be returned, which would make sense universally.
Scenarios 1 and 3 highlight the issue of using an array as implemented by @evanshortiss.
evanshortiss commentedon Oct 19, 2016
@hacksparrow here are some thoughts.
I think populating with req.route makes sense, but this example (app.use) is definitely a valid gap, since you do actually respond in the final handler but it (req.matchedRoutes) would yield no matches in this case if we were to use req.route. However, I would argue that you should use app.all('/secrets', handler) or app.METHOD('/secrets', handler) for the last scenario since this would populate req.route and is more sensible from a semantic standpoint. For middleware such as body parser, nothing should be added to the matched routes, since they aren't routes, but are a passthrough.
a. Yes, one would expect this is populating the Array using req.route and app.all: [{path: '/secret', /* other req.route props */}], or the current PR [ '/', '/', '/', '/secret', '/secret' ].
b. This information can be useful at runtime for logging.
True, but it's a passthrough middleware. The purpose of this issue is purely to get the matched routes, not the state of the request object.
Correct, but again these are passthroughs/middleware. One can delete known passthroughs to easily construct the absolute URL, or use them if their scenario requires.
Ultimately the goal of this issue is to enable a developer to programatically determine matched routes which seems like a reasonable request. But I agree, we can see it's a challenge to achieve in a manner that works "universally" and raises the question of can and/or should express attempt provide this.
hacksparrow commentedon Oct 19, 2016
@evanshortiss yes, I agree it is a reasonable request. It's just that the API is not very convincing or a lot of complexities will be involved (eg: detecting passthroughs). It would be great to have a really elegant solution.
👍 for "This information can be useful at runtime for logging", hadn't thought about it.
danieljuhl commentedon Oct 20, 2016
@hacksparrow I think the purpose of this PR is to get the routes matched not the 'route handlers' matched. The purpose of this PR, IMHO, is to get the 'context', and not just the final route matched. I do aknowledge that it can look messy if you have a very complex route structure, but I can't see how you can make something messy, look elegant - at least not for now :)
dougwilson commentedon Oct 21, 2016
Hi @danieljuhl which PR are you referring to? This is just an issue and references a few PRs, so just trying to get context on your statement.
danieljuhl commentedon Oct 21, 2016
@dougwilson - sorry, PR #3100 as referenced by @hacksparrow
dougwilson commentedon Oct 21, 2016
Gotcha. I was thinking that, @danieljuhl , but all the examples in the #3100 seem to indicate the intention of the PR is to build the final route matched, which seems to conflict what your description of your PR. Perhaps I'm just misunderstanding here?
evanshortiss commentedon Nov 7, 2016
@dougwilson @danieljuhl correct, it's to get context. For me personally I'd like to construct a string that represents the matched URL. @hacksparrow has a nice solution here that would allow one to do what my PR does, without technically adding it as a feature to express.
shivasurya commentedon Jul 2, 2019
any solution for this will make me happy ;)
snawaz commentedon Jul 24, 2019
Another use case would be when supporting Prometheus metrics for all endpoints. To do that, one could use middleware functions at the app-level and use the
originalRouteas value of one of the label, something like this:Since
req.urlcould have user-id, tokens, etc, in it, we cannot use it as the values as that way the number of time-series would be too many.. Not good. So I've kinda this workaround:.. where
normalizeis a function which finds & replaces variable-values such as uuid, tokens, etc with something constant like-and just empty-string. It pointlessly consumes some CPU time. We could avoid paying for this if we haveoriginalRoute, ororiginalPath(whichever sounds good).wesleytodd commentedon Jul 24, 2019
The router has been moved, and this would be a feature of the router. You can see what IMO is the best option for this on the
Routerrepo: pillarjs/router#34Any future conversation should be had over on that thread, as it is where we would make this kind of change.
manvydasu commentedon Aug 20, 2022
So it's impossible to have metrics for "response times by endpoint" using express with the current implementation of router?
Merge PR #73: Add req.originalRoute, the full matched route pattern (e…