Node.js - 在生产模式下动态禁止某些公共路径

问题描述 投票:0回答:1

我正在使用带有express.js的node.js。 这是我的公共目录结构:

public/
    js/
    css/
    images/
    built/
        main-built.js
        main-built.css
    index.html
    dev.html

其中index.html链接到js/css/脚本,而dev.html链接到main-built.jsmain-built.css 。 当应用程序作为生产模式启动时,如何在public/js/*css/*dev.html )下动态禁止某些路径?

node.js path public production restrict
1个回答
2
投票

如果您使用的是Express,则可以使用一小块中间件阻止它们。

app.configure(function() {
  app.use(function(req, res, next) {
    if(process.env.NODE_ENV === 'production') {
      if( /* check req.url for blocked content */) {
        res.send('Access Denied');
      }
    }
    else {
      // Not in production, proceed to next handler.
      next();
    }
  });
  app.use(express.static(__dirname + '/public'));
});
© www.soinside.com 2019 - 2024. All rights reserved.