重定向到express.js中的静态文件 [英] Redirecting to a static file in express.js
问题描述
在Express.java中的Express中,我想检查特定路径下的请求(例如 / restricted
),如果可以接受请求,请求由处理缓存头等的静态提供者处理。
In Express under Node.js, I'd like to inspect a request under a specific path (say, /restricted
) and if it is acceptable, have the request be handled by the static provider, which handles the caching headers etc.
如果我只是使用 app.get('/ restricted /:file' ,...)
然后使用 res.sendfile
发送静态文件,如果被批准,它将忽略任何缓存头,并始终发送文件
If I simply use app.get('/restricted/:file', ...)
and then use res.sendfile
to send the static file if approved, it will ignore any caching headers and always send the file.
由于不同的用户只能获取不同的文件,因此我无法使用全面的登录支票。
I can't use a blanket logged-in check because different users should only get different files.
实现这个的最好方法是什么?
What is the best way to implement this?
推荐答案
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);
这将在适当时使用静态中间件,包括其所有功能。否则,您可以酌情处理未经授权的请求。
This will use the static middleware when appropriate including all of its functionality. Otherwise you can handle unauthorized requests as appropriate.
这篇关于重定向到express.js中的静态文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!