URL Rewriting with ExpressJS

前端 未结 5 1366
灰色年华
灰色年华 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:23

    A solution that works without response.sendfile(..) is to use a rewrite middleware that is inserted prior to app.use(express.static(..)) like this:

    // forward all requests to /s/* to /index.html
    
    app.use(function(req, res, next) {
    
      if (/\/s\/[^\/]+/.test(req.url)) {
        req.url = '/index.html';
      }
    
      next();
    });
    
    // insert express.static(...)
    

    This way, expressjs properly recognizes the rewrite. The static middleware will then take care of serving the file.

提交回复
热议问题