我有一个Vue项目,我所有的路线都是相同的结构,然而,其中一个总是给我一个未经授权的错误。
路线如下:
router.get('/:userId/reviews', checkJwt, verifyUser, controller.getReviewsByUserId)
router.put('/:userId/review', checkJwt, verifyUser, controller.updateReview)
router.delete('/:userId/review', checkJwt, verifyUser, controller.removeReviewFromUser) // this is the broken route
如果我将断开的路线更改为:
router.put('/:userId/delete/review', checkJwt, verifyUser, controller.removeReviewFromUser)
那么它就工作得很好。checkJwt
中间件是问题的来源。但是我有10个其他路由,它们的结构与失败的delete
路由完全相同。为什么?
下面是checkJwt
代码,尽管这不应该是问题所在,因为只要http动词是put
、post
或' get
,它对我的所有其他15个路由都有效:
const { auth } = require('express-oauth2-jwt-bearer')
// Setup auth
const checkJwt = auth({
audience: 'MY-AUDIENCE',
issuerBaseURL: 'https://simplyadvanced.auth0.com/'
})
module.exports = {
checkJwt
}
下面是完整的错误消息:
UnauthorizedError: Unauthorized
at getToken (C:\Users\cody\App\app-core\api\node_modules\express-oauth2-jwt-bearer\dist\index.js:83:15)
at C:\Users\cody\App\app-core\api\node_modules\express-oauth2-jwt-bearer\dist\index.js:353:25
at Layer.handle [as handle_request] (C:\Users\cody\App\app-core\api\node_modules\express\lib\router\layer.js:95:5)
at next (C:\Users\cody\App\app-core\api\node_modules\express\lib\router\route.js:137:13)
at Route.dispatch (C:\Users\cody\App\app-core\api\node_modules\express\lib\router\route.js:112:3)
at Layer.handle [as handle_request] (C:\Users\cody\App\app-core\api\node_modules\express\lib\router\layer.js:95:5)
at C:\Users\cody\App\app-core\api\node_modules\express\lib\router\index.js:281:22
at param (C:\Users\cody\App\app-core\api\node_modules\express\lib\router\index.js:360:14)
at param (C:\Users\cody\App\app-core\api\node_modules\express\lib\router\index.js:371:14)
at Function.process_params (C:\Users\cody\App\app-core\api\node_modules\express\lib\router\index.js:416:3)
1条答案
按热度按时间n6lpvg4x1#
解决了这个问题。DELETE请求不应该有请求主体,所以通过删除主体并将ID添加到URL请求参数中,它就可以工作了。