我正在使用node.js并将其表示为route方法,我的路线如下所示:
Set the website routes
app.use('/public', express.static('./public'));
app.use('/web', express.static('./web'));
我如何在一种方法中设置限制对“公共”和“ Web”文件夹的访问,目前我使用的是两行代码
app.get('/public*', checkPermissions, function(req,res,next){ next(); });
app.get('/web*', checkPermissions, function(req,res,next){ next(); });
该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(); });
});
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句