Skip to content

Store matched routes in request#2864

Closed
ajfranzoia wants to merge 1 commit intoexpressjs:masterfrom
ajfranzoia:feat-matched-routes
Closed

Store matched routes in request#2864
ajfranzoia wants to merge 1 commit intoexpressjs:masterfrom
ajfranzoia:feat-matched-routes

Conversation

@ajfranzoia
Copy link
Copy Markdown

@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
Copy Markdown
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
Copy Markdown

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
Copy Markdown
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
Copy Markdown
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