我正在编写API,我需要将以下2条路由绑定到相同的控制器操作:
'GET /user/count': 'UserController.getUserCount',
'GET /user/count/:role': 'UserController.getUserCount',在操作中,我希望检查是否指定了角色,如果没有指定角色,则发送总用户计数,如果指定了角色,则给出角色的用户计数。控制器操作如下:
getUserCount: function(request, response){
var role = request.params.role;
var query = {};
if (role != null){
query.role = role;
}
User.count(query).exec(function countCB(error, count) {
console.log('There are ' + count + ' users.');
response.json(count);
});
}, 这有可能实现吗?
发布于 2017-01-24 12:16:48
是的,这是允许的。
在将多个路由绑定到相同的控制器操作中没有问题。
https://stackoverflow.com/questions/41819189
复制相似问题