我试图拒绝公众使用node express在特殊目录中查看文件,以下是代码:

app.use(express.static(path.join(__dirname, 'partials')));

app.all('/partials/*', function (req,res, next) {

    res.status(403).send(
    {
        message: 'Access Forbidden'
    });
    next();
});

如果我路由到localhost/partials,则会收到消息“禁止访问”,但是如果我路由到localhost/partials/files.html,则不会

有什么建议吗?

最佳答案

语句的顺序在node.js中很重要。

app.all('/partials/*', function (req,res, next) {
   res.status(403).send({
      message: 'Access Forbidden'
   });
});

//this line is used to load resources at localhost/partials/api.html
//but, because of above code snippets, we have blocked /partials/* routes
//hence, this line will practically wont work.
app.use('/partials',express.static(path.join(__dirname, 'partials')));

10-04 20:32