简体   繁体   中英

How to configure express.js's `app.use(express.static(…)`?

I'm using express.js's app.use(express.static(...)) to serve my files. I'd like to configure some restricted files and make routings (such as redirecting requests ending in .png to a specific folder). Is this possible?

You can't get what you need from the express.static middleware, however most of these are easy to implement.
To setup restricted files, simply put a middleware above your express.static middleware in the gist of:

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM