Node.js 如何使用express jwt发送自定义错误响应?
我想知道在未经授权的情况下如何更改express jwt的响应,我尝试了错误处理程序,但它不起作用 这是我的验证功能Node.js 如何使用express jwt发送自定义错误响应?,node.js,oauth,express-jwt,Node.js,Oauth,Express Jwt,我想知道在未经授权的情况下如何更改express jwt的响应,我尝试了错误处理程序,但它不起作用 这是我的验证功能 const checkJwt = jwt({ secret: jwksRsa.expressJwtSecret({ cache: true, rateLimit: true, jwksRequestsPerMinute: 5, jwksUri: `https://dev-88888.com/.well-known/jwks.json
const checkJwt = jwt({
secret: jwksRsa.expressJwtSecret({
cache: true,
rateLimit: true,
jwksRequestsPerMinute: 5,
jwksUri: `https://dev-88888.com/.well-known/jwks.json`
}),
audience:'https://dev-******.com/api/v2/',
issuer: `https://dev000000.com/`,
algorithms: ['RS256']
});
这就是我得到的
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Error</title>
</head>
<body>
<pre>UnauthorizedError: invalid token
<br> at /Users/luisandrade/code/slothy_/back/node_modules/express-jwt/lib/index.js:102:22
<br> at Object.module.exports [as verify] (/Users/luisandrade/code/slothy_/back/node_modules/jsonwebtoken/verify.js:75:12)
<br> at verifyToken (/Users/luisandrade/code/slothy_/back/node_modules/express-jwt/lib/index.js:100:13)
<br> at fn (/Users/luisandrade/code/slothy_/back/node_modules/express-jwt/node_modules/async/lib/async.js:746:34)
<br> at /Users/luisandrade/code/slothy_/back/node_modules/express-jwt/node_modules/async/lib/async.js:1213:16
<br> at /Users/luisandrade/code/slothy_/back/node_modules/express-jwt/node_modules/async/lib/async.js:166:37
<br> at /Users/luisandrade/code/slothy_/back/node_modules/express-jwt/node_modules/async/lib/async.js:706:43
<br> at /Users/luisandrade/code/slothy_/back/node_modules/express-jwt/node_modules/async/lib/async.js:167:37
<br> at Immediate._onImmediate (/Users/luisandrade/code/slothy_/back/node_modules/express-jwt/node_modules/async/lib/async.js:1206:34)
<br> at runCallback (timers.js:810:20)
<br> at tryOnImmediate (timers.js:768:5)
<br> at processImmediate [as _immediateCallback] (timers.js:745:5)
</pre>
</body>
</html>
非常感谢您的帮助。从文档中我了解到,您可以使用
express
传递自定义中间件以进行错误处理
{
Error: 'Some Error message'
}
app.use(function (err, req, res, next) {
if (err.name === 'UnauthorizedError') {
res.status(401).send('invalid token...');
}
})