[英]How to configure express.js's `app.use(express.static(…)`?
I'm using express.js's app.use(express.static(...))
to serve my files. 我正在使用express.js的app.use(express.static(...))
提供文件。 I'd like to configure some restricted files and make routings (such as redirecting requests ending in .png
to a specific folder). 我想配置一些受限制的文件并进行路由(例如将以.png
结尾的请求重定向到特定文件夹)。 Is this possible? 这可能吗?
You can't get what you need from the express.static middleware, however most of these are easy to implement. 您无法从express.static中间件中获得所需的东西,但是其中大多数都很容易实现。
To setup restricted files, simply put a middleware above your express.static middleware in the gist of: 要设置受限制的文件,只需按照以下要旨将一个中间件放在您的express.static中间件之上:
app.use(function(req,res,next){
if (req.url == "/path/to/bad/file") res.send(403)
else next();
})
to setup redirection (using the same type of middleware), you have two options either perform res.redirect("real path") or send the file yourself using res.sendFile("path"), which is more or less what express.static does. 要设置重定向(使用相同类型的中间件),您有两个选择或者执行res.redirect(“ real path”)或使用res.sendFile(“ path”)自己发送文件,这或多或少是表达的。静态的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.