pillarjs / router

Simple middleware-style router
MIT License
409 stars 102 forks source link

Provide access to original unparsed path #85

Open GuskiS opened 5 years ago

GuskiS commented 5 years ago

As discussed in https://github.com/expressjs/express/issues/4084 there is a need for original path on req object.

Example:

router.get("/user/:id", (req, res, next) => {
  console.log(req.originalUnparsedPath);  // => "/user/:id"
  next();
});

originalUnparsedPath is just an example, but the name would be something along the lines.

dougwilson commented 5 years ago

Hi! Thanks for the feature request. Can you provide more information around what you expect to use this for? For example, if the route is defined as

router.get(["/user/:id", "/person/:id"], (req, res, next) => {
  console.log(req.originalUnparsedPath);  // => "/user/:id"
  next();
});

would the expectation here be (a) you get that original array back (b) you get just the element in the array that matched the request url or (c) something else?

The feature request to too vague on what the goal is for the usage of this data is to gauge, if you can expand on what specific you're going to use it for as well. For example, what should it provide when there are routers mounted in routers?

GuskiS commented 5 years ago

Thanks for reply. I wasn't aware you can use router API like that. That being said, I would expect req.originalUnparsedPath to work same way as req.originalUrl do, but just return the unparsed value.

router.get(["/user/:id", "/person/:id"], (req, res, next) => {
  console.log({ original: req.originalUrl, unparsed: req.originalUnparsedPath });
  next();
});
// => { original: "/user/123", unparsed: "/user/:id" }
// or
// => { original: "/person/123", unparsed: "/person/:id" }
// depending on request you make

I need to log it out and then create AWS metrics.

wesleytodd commented 5 years ago

Does this PR satisfy this? https://github.com/pillarjs/router/pull/34

GuskiS commented 5 years ago

I suppose that would work, yes.

donoftime2018 commented 12 months ago

I cannot access originalUnparsedPath. I have express 4.18.2