当前位置: 首页 > 后端技术 > Node.js

Fastify.js-configureContent-Type接收缓冲区,配置JWT

时间:2023-04-03 21:11:28 Node.js

最近在开发unPKG之类的代码库,选择了Fastify.js的框架,相对于express.js而言,框架比较小。配置Content-Type时,并不总能获取到buffer二进制流。//添加内容类型解析器-添加对缓冲区的支持配置secret//配置jwtfastify.register(jwt,{secret:'test'})获取Token//monitor-registertokenfastify.post('/sign',async(request,reply)=>{consttoken=awaitreply.jwtSign(request.body)reply.send({token:token})})decryptToken拦截所有的onRequest生命周期,每次获取请求时解密。然后返回监听器fastify.addHook('onRequest',async(request,reply)=>{try{awaitrequest.jwtVerify()}catch(err){reply.send(err)}})