阻止直接访问Node&中的目录.快递应用 [英] Prevent direct access to directory in Node & Express app

查看:35
本文介绍了阻止直接访问Node&中的目录.快递应用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的Express应用程序中,我在/public中有一个名为media的目录,并且我想限制对该目录(以及它的子目录)的访问,重定向或显示404.

In my express app i have a directory called media in /public, and i want to restrict access to it (and it's sub-directories as well), redirect or display a 404.

我该如何实现?预先感谢.

How i can achieve it? Thanks in advance.

推荐答案

我想出了我自己问题的答案.

I figured out the answer to my own question.

app.all('/media/*', (req,res, next) {
    res.status(403).send({
       message: 'Access Forbidden'
    });
    // or whatever
});
app.use('/media',express.static(path.join(__dirname, 'media')));

这篇关于阻止直接访问Node&中的目录.快递应用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆