Rewrite url path using node.js

后端 未结 3 1719
名媛妹妹
名媛妹妹 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:37

    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

提交回复
热议问题