我在Nodejs API中使用expressJWT来授权特定的端点。当用户未使用有效令牌时,expressJWT将返回UnauthorizedError。是否可以处理多个错误?例如:检查SELECT t.week, SUBSTRING_INDEX(GROUP_CONCAT(t.votedRestaurant ORDER BY t.total DESC),',',1) votedRestaurant, MAX(t.total)
FROM(
SELECT votedRestaurant, WEEK, COUNT(*) total
FROM votes
GROUP BY votedRestaurant,WEEK
ORDER BY WEEK DESC
) t
GROUP BY t.week
ORDER BY t.week DESC
或error.name === 'BadRequest'
等。这就是我的代码:
error.name === 'OtherError'
// Require authentication for every request,
// unless the path is specified below.
app.use(expressJWT({
secret: config.secretKey
}).unless({
path: ['/api/login/psychologist', '/api/login/client', '/api/register/client', '/api/register/psychologist']
}), function (error, req, res, next) {
if (error.name === "UnauthorizedError") {
const err = Errors.unauthorized();
res.status(err.code).json(err);
}
});
是自定义错误。
因此,如果可以处理多个错误,我的代码将如下所示:
Errors.unauthorized();