我需要从处理程序文件访问 fastify 实例。我根本不记得我应该怎么做。
指数:
fastify.register(require('./routes/auth'), {
prefix: '/auth'
})
Run Code Online (Sandbox Code Playgroud)
路线/授权:
module.exports = function(fastify, opts, next) {
const authHandler = require('../handlers/auth')
fastify.get('/', authHandler.getRoot)
next()
}
Run Code Online (Sandbox Code Playgroud)
处理程序/授权:
module.exports = {
getRoot: (request, reply) {
// ACCESS FASTIFY NAMESPACE HERE
reply.code(204).send({
type: 'warning',
message: 'No content'
})
}
}
Run Code Online (Sandbox Code Playgroud)
谢谢!
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
Run Code Online (Sandbox Code Playgroud)
以下是请求的 OnRequest 挂钩的更新用法:
fastify.decorateRequest('fastify', null)
fastify.addHook("onRequest", async (req) => {
req.fastify = fastify;
});
Run Code Online (Sandbox Code Playgroud)
如果需要回复,请将“onRequest”替换为“onReply”。 请参阅此处的 Fastify 文档。
路线/授权:
module.exports = function(fastify, opts, next) {
const authHandler = require('../handlers/auth')(fastify)
fastify.get('/', authHandler.getRoot)
next()
}Run Code Online (Sandbox Code Playgroud)
处理程序/授权:
module.exports = function (fastify) {
getRoot: (request, reply) {
fastify;
reply.code(204).send({
type: 'warning',
message: 'No content'
})
}
}Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
6256 次 |
| 最近记录: |