我有一个应用程序使用 express.static 来访问一些用户信息,路由逻辑是这样的:
app.get('/principal',function(req,res,next){
if(req.query.user=='admin'){
next();
}else{
res.send("not allowed");
}
});
app.use('/principal',express.static('/_site'));
但现在我需要它提供给所有用户,所以应该是这样的:
var username = '';
app.get('/principal',function(req,res,next){
if(req.query.user){
username = req.query.user;
next();
}else{
res.send("not allowed");
}
});
app.use('/principal',express.static(username+'/_site'));
我的每个用户名都有一个文件夹,我的 express.static() 一直读取用户名 var 为空。
最佳答案
尝试从路由处理程序提供静态文件,使其动态到查询用户名:
app.get('/principal',function(req,res,next){
if(req.query.user){
// serve static files for usename
app.use('/principal', express.static(username +'/_site'));
res.send("allowed");
}else{
res.send("not allowed");
}
});
关于node.js - 动态 express.static() 文件夹路径,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48623768/