我使用node.js和express作为路由方法,我的路线如下:
Set the website routes
app.use('/public', express.static('./public'));
app.use('/web', express.static('./web'));
如何在一种方法中设置对“公共”和“网络”文件夹的限制访问权限,目前我使用的是两行,此代码
app.get('/public*', checkPermissions, function(req,res,next){ next(); });
app.get('/web*', checkPermissions, function(req,res,next){ next(); });
答案 0 :(得分:1)
checkPermissions
函数应如下所示:
function checkPermissions(req, res, next) {
// logic to check whether user has permissions or not.
// example:
if (req.user.permissions == 'admin') {
next();
} else {
// redirect if user doesn't have permission.
res.redirect('/no-permissions');
}
}
如果你真的想让它适合1行(取决于lodash或下划线):
_.(['/public*', '/web*']).each(function(route) {
app.get(route, checkPermissions, function(req, res, next) { next(); });
});