Rewrite url path using node.js

express, node.js

Solution

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)`.

Problem

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

Original source

Related problems