到目前为止,我的路线被包含在内:
router.use('/groups', require('./routes/system/groups'));
是否可以将函数放在第二个参数上并进行如下操作:
router.use('/sustav/groups', function(req, res){
switch (req.user.role){ // role I have in user already
case "admin": {
return require('./routes/system/admin/groups');
}
case "user": {
return require('./routes/system/user/groups');
}
}
});
像这样,我可以根据用户角色分离控制器。
答案 0 :(得分:1)
app.use('/sustav/groups', require('./routes/system/admin/groups'));
app.use('/sustav/groups', require('./routes/system/user/groups'));
<强> ./路由/系统/管理/组强>
...
if(req.user.role != 'admin') {
return next();
}
...
<强> ./路由/系统/用户/组强>
...
if(req.user.role != 'user') {
return next();
}
...