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

Store matched routes in request #2864

Closed
wants to merge 1 commit into from

Conversation

ajfranzoia
Copy link

@dougwilson I've been working on this feature (#2501).
It allows to store every matched route in a request when travelling through nested routes. This may be useful for logging, ACL checks, etc.
So, for example, if you have your routing set like this:

var fooRouter = new Router();
var barRouter = new Router();
var bazRouter = new Router();

bazRouter.get(['/bez', '/baz/:subId'], function(req, res, next) {
  next();
});

fooRouter.use(['/foo/:id', '/foe'], barRouter);
barRouter.use(['/bar'], bazRouter);

A request for the URL /foo/10/bar/baz/30 will set an array of matched routes in the request object like this:

req.matchedRoutes = ['/foo/:id', '/bar', '/baz/:subId'];

Let me know if there is anything that needs to be modification, unmeaningful variable names, etc.

@ajfranzoia
Copy link
Author

Sorry for mentioning you @dougwilson, my mistake.
I will create a similar PR on your the pillarjs/router if you think is a feature that could be added.

@jasnell
Copy link

jasnell commented Jan 25, 2016

@ajfranzoia ... nice! was just asked about this kind of thing at a recent node meetup. good stuff :-)

@dougwilson
Copy link
Contributor

Hi @ajfranzoia, thank you for your pull request!

Unfortunately this pull request itself is not in a state that would be backwards-compatible with Express 4, which is the code your pull request is targeting. Express has been so API stable for such a long time, it's pretty much impossible to change any public API surface, or what seems like non-public API surface without breaking lots of production apps. Of note, it removes the .regexp and .key properties from all route objects. A lot of third-party code walks these objects to build route lists, etc.

The actual idea seems good, but should be made against 5.0, which does not even have route code. If you want, please transplant this pull request to see https://github.com/pillarjs/router for the discussion to open up there, where other router enthusiasts are as well :)

@ajfranzoia
Copy link
Author

Thanks for your reply @dougwilson. I will definitly create a PR against https://github.com/pillarjs/router during the next days.

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

Successfully merging this pull request may close these issues.

3 participants