I need to access the fastify instance from a handler file. I don't remember at all how I should be doing that.
index:
fastify.register(require('./routes/auth'), {
prefix: '/auth'
})
routes/auth:
module.exports = function(fastify, opts, next) {
const authHandler = require('../handlers/auth')
fastify.get('/', authHandler.getRoot)
next()
}
handler/auth:
module.exports = {
getRoot: (request, reply) {
// ACCESS FASTIFY NAMESPACE HERE
reply.code(204).send({
type: 'warning',
message: 'No content'
})
}
}
Thanks!
fastify.decorateRequest('fastify', fastify); Will now return a warning message:
FastifyDeprecation: You are decorating Request/Reply with a reference type. This reference is shared amongst all requests. Use onRequest hook instead. Property: fastify
Here is the updated usage for the OnRequest hook for requests:
fastify.decorateRequest('fastify', null)
fastify.addHook("onRequest", async (req) => {
req.fastify = fastify;
});
Replace "onRequest" with "onReply" if needed for replies. See Fastify docs on it here.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With