Rewrite url path using node.js

后端 未结 3 1716
名媛妹妹
名媛妹妹 2020-12-16 14:26

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\';
3条回答
  •  悲哀的现实
    2020-12-16 14: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).

提交回复
热议问题