19

Is it possible to rewrite the URL path using node.js?(I'm also using Express 3.0)

I've tried something like this:

req.url = 'foo';

But the url continues the same

Rafael Motta
  • 2,328
  • 2
  • 19
  • 18

3 Answers3

47

Sure, just add a middleware function to modify it. For example:

app.use(function(req, res, next) {
  if (req.url.slice(-1) === '/') {
    req.url = req.url.slice(0, -1);
  }
  next();
});

This function removes the trailing slash from all incoming request URLs. Note that in order for this to work, you will need to place it before the call to app.use(app.router).

David Weldon
  • 63,632
  • 11
  • 148
  • 146
  • 3
    I think it is working fine, but the path on the browser continues the same... Is it possible to rewrite the URL on the user browser too or just using res.redirect ? – Rafael Motta Nov 19 '12 at 00:42
  • 5
    If you want to modify the URL in the browser, I think you need to redirect the user. See the answer and comments [here](http://stackoverflow.com/questions/13442377/redirect-all-trailing-slashes-gloablly-in-express/). – David Weldon Nov 19 '12 at 00:53
  • 8
    In Express 4 (and maybe earlier), this no longer works. The `Request` instance defines the `req` property as not `writable`. – Bailey Parker Aug 04 '15 at 18:28
  • 3
    @PhpMyCoder could you add an answer showing how to do this in more modern versions? I haven't been using express for a while so I'm not sure how to fix this. – David Weldon Aug 04 '15 at 18:36
  • 7
    Turns out I can't read. For some reason when I skimmed over this question I read `req.path` not `req.url`. `req.url` is a property of `Http.IncomingMessage` so it can be set. Your original answer was correct. My apologies! – Bailey Parker Aug 04 '15 at 18:41
  • 1
    @BaileyParker What did you end up doing since `req.url` is no longer writable? – Joshua Robinson Oct 07 '16 at 10:04
  • 1
    Specifically on removing end slashes, I don't think this solution will work if the URL has a query string that needs to be preserved. For that use-case, consider checking [`req.path`](http://expressjs.com/en/api.html#req.path) instead and reconstructing the URL using the modified path + query string. – tavnab Dec 19 '16 at 00:55
2

A good idea should be to update the path too. My method suggestions:

app.use(function(req, res, next) {
    console.log("request", req.originalUrl);
    const removeOnRoutes = '/not-wanted-route-part';
    req.originalUrl = req.originalUrl.replace(removeOnRoutes,'');
    req.path = req.path.replace(removeOnRoutes,'');
    return next();
});

By this way /not-wanted-route-part/users will became /users

Tiago Gouvêa
  • 15,036
  • 4
  • 75
  • 81
1

you need override some properties of req (IncomingMessage in NodeJs):

  • url
  • path
  • search
  • query

2

function changeUrl(req, url) {
    const parsedUrl = new URL(url);
    req.url = url;
    req.originalUrl = url;
    req.path = parsedUrl.pathname;
    req.search = parsedUrl.search;
    req._parsedUrl = parsedUrl;
    const query = {};
    for(const entry of parsedUrl.searchParams) {
        query[entry[0]] = entry[1];
    }
    req.query = query;
}
Sergey Gurin
  • 1,537
  • 15
  • 14