URL Rewriting with ExpressJS

前端 未结 5 1365
灰色年华
灰色年华 2020-12-05 04:37

I would like to rewrite my URLs on my ExpressJS website. I\'ve used this plugin, https://github.com/joehewitt/express-rewrite, but it doesn\'t work...

However, I mig

5条回答
  •  情书的邮戳
    2020-12-05 05:02

    You could rewrite the URL before you get to the handler you want to use.

    app.use(function(req, res, next) {
       if (req.url === '/toto') {
         req.url = '/heytoto';
       }
       next();
    });
    
    app.get('/heytoto', ...);
    

    I've used a similar method to do URL rewrites with regular expressions.

提交回复
热议问题