我需要从处理程序文件中访问fastify实例。我一点也不记得我该怎么做。
索引:
fastify.register(require('./routes/auth'), {
prefix: '/auth'
})路线/航次:
module.exports = function(fastify, opts, next) {
const authHandler = require('../handlers/auth')
fastify.get('/', authHandler.getRoot)
next()
}处理程序/auth:
module.exports = {
getRoot: (request, reply) {
// ACCESS FASTIFY NAMESPACE HERE
reply.code(204).send({
type: 'warning',
message: 'No content'
})
}
}谢谢!
发布于 2021-10-18 21:39:52
fastify.decorateRequest('fastify',fastify);现在将返回一个警告消息:
FastifyDeprecation: You are decorating Request/Reply with a reference type. This reference is shared amongst all requests. Use onRequest hook instead. Property: fastify下面是请求的OnRequest钩子的更新用法:
fastify.decorateRequest('fastify', null)
fastify.addHook("onRequest", async (req) => {
req.fastify = fastify;
}); 如果需要回复,请将"onRequest“改为"onReply”。请看这里的Fastify文档。
https://stackoverflow.com/questions/52939927
复制相似问题