nodejs定义token中间件

//一定要在路由之前,配置解析token的中间件

const  expressJWT=require('express-jwt')
const  config=require('./common/config')
app.use(expressJWT({
    
    secret:config.jwtScretKey}).unless({
    
    path:[/^\/api/]}))

//定义错误级别文件

if(err.name=='UnauthorizedError') return res.cc('身份认证失败!')
app.use((err,req,res,next)=>{
    
    
	//验证失败导致的错误
	if(err  instanceof joi.ValidationError) return  res.cc(err)
	//身份认证失败的错误
	if(err.name=='UnauthorizedError') return res.cc('身份认证失败!')
	//未知错误
	res.cc(err)
})

猜你喜欢

转载自blog.csdn.net/weixin_45932157/article/details/125837266