从Express中的静态文件中排除子目录 [英] Exclude sub directory from static files in express
问题描述
有什么方法可以从 express 4.8.5中的Express静态中间件中排除子目录.
Is there any way to exclude sub directory from express static middleware in express 4.8.5.
例如,如果我有:
app.use(express.static(__dirname + 'public'));
我的公共目录是这样的:
And my public directory is like this :
- public
- css
- images
- scripts
- index.html
- exclude_me
- scripts
- views
- index.html
因此,我需要排除最后一个子目录以及用户执行操作的时间:
So I need to exclude last sub directory and when user does :
GET /exclude_me
它应该呼叫我的路线,而不是自动返回目录.
It should call my route rather than returning directory automatically.
我不能仅仅将其从公共目录中删除,因为它取决于其中的内容,因为 public 目录是有角度的应用程序,而 exclude_me 是另一个从中获取脚本的有角度的应用程序/exclude_me/scripts 和 AND ,来自/public/scripts .
I can't just remove it from public dir because it depends on stuff inside it because public directory is angular application and exclude_me is another angular application that fetches scripts from /exclude_me/scripts AND from /public/scripts.
我知道这有点让人困惑,但是它是这样,我不能仅仅将其从公共目录中删除,因为它不再看到需要的 public/scripts 并且我不能离开它因为那时我无法对其进行授权(所有授权均在 public/scripts 中)
I know it is little confusing but it is how it is and I cannot just remove it from public dir because it won't see public/scripts any more which are needed and I cannot leave it because I cannot authorize it then (all authorization is in public/scripts)
如果有更聪明的方法,请随时告诉我:)
If there is some smarter way to do this, feel free to let me know :)
推荐答案
您可以添加自己的中间件.这是我为排除某些文件夹所做的事情:
You can add your own middleware. Here's what I did to exclude some folders:
app.use('/public', (req, res, next) => {
if (env !== 'development') {
var result = req.url.match(/^\/js\/(maps|src)\/.+\.js$/)
if (result) {
return res.status(403).end('403 Forbidden')
}
}
next()
})
app.use('/public', express.static(path.join(__dirname, '/public')))
这篇关于从Express中的静态文件中排除子目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!