简体   繁体   English

如何配置express.js的app.use(express.static(…)`?

[英]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.

相关问题 如何关闭app.use(express.static('/ public')); 快递js - How to turn off app.use(express.static('/public')); Express js app.use(“ /”,express.static)和app.use(express.static)之间有区别吗? - Is there a difference between app.use(“/”, express.static) and app.use(express.static)? 如果app-是子应用程序,则`app.use(express.static`似乎不起作用 - `app.use(express.static` seems to not working if app - is subapplication 使用 app.use(express.static('public')) 时如何测试 Express 应用程序; - How to test an Express application when using app.use(express.static('public')); app.use(express.raw()) 在 Express.js 中不起作用 - app.use(express.raw()) not working in Express.js 如何在app.use express中配置强大? - how to configure formidable in app.use express? Express.js“app.use()需要中间件功能” - Express.js “app.use() requires middleware function” 我的 javascript 文件没有链接? 我正在使用: app.use(express.static(__dirname + '/public')); - my javascript file not linking? Iam using: app.use(express.static(__dirname + '/public')); 与 app.use(express.static(...)) 相关的 next() 中间件/路由是什么? - What are the next() middlewares/routes relative to app.use(express.static(…))? app.use(express.static("public")) 是否为每个请求调用中间件? - Does app.use(express.static("public")) call the middleware for every request?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM