我试图拒绝公众使用 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')));
关于node.js - 拒绝访问nodejs express中的特定目录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37457676/