redirect到express.js中的静态文件
在Node.js下的Express中,我想检查一个特定path下的请求(比如说/restricted
),如果可以接受的话,请求由静态提供者处理,静态提供者处理caching头。
如果我简单地使用app.get('/restricted/:file', ...)
,然后使用res.sendfile
发送静态文件(如果获得批准),它将忽略任何caching标题并始终发送文件。
我不能使用全面login检查,因为不同的用户应该只能得到不同的文件。
什么是实施这个最好的方法?
var express = require("express"); var app = express.createServer(); var staticMiddleware = express.static(__dirname + "/public"); app.get("/restricted/:file", function(req, res, next) { var authorized = true; //Compute authorization appropriately here if (authorized) { staticMiddleware(req, res, next); } else { res.send(401); } }); app.listen(3456);
这将在适当时使用静态中间件,包括其所有function。 否则,您可以酌情处理未经授权的请求。