Hooks
Hooks
Hooks are registered with the fastify.addHook method and allow you to listen
to specific events in the application or request/response lifecycle. You have to
register a hook before the event is triggered, otherwise, the event is lost.
By using hooks you can interact directly with the lifecycle of Fastify. There are Request/Reply hooks and application hooks:
- Request/Reply Hooks
- Application Hooks
- Scope
- Route level hooks
- Using Hooks to Inject Custom Properties
- Diagnostics Channel Hooks
Notice: the done callback is not available when using async/await or
returning a Promise. If you do invoke a done callback in this situation
unexpected behavior may occur, e.g. duplicate invocation of handlers.
Request/Reply Hooks
Request and Reply are the core Fastify objects.
done is the function to continue with the lifecycle.
It is easy to understand where each hook is executed by looking at the lifecycle page.
Hooks are affected by Fastify's encapsulation, and can thus be applied to selected routes. See the Scopes section for more information.
There are eight different hooks that you can use in Request/Reply (in order of execution):
onRequest
fastify.addHook('onRequest', (request, reply, done) => {
// Some code
done()
})
Or async/await:
fastify.addHook('onRequest', async (request, reply) => {
// Some code
await asyncMethod()
})
Notice: in the onRequest hook, request.body will always be
undefined, because the body parsing happens before the
preValidation hook.
preParsing
If you are using the preParsing hook, you can transform the request payload
stream before it is parsed. It receives the request and reply objects as other
hooks, and a stream with the current request payload.
If it returns a value (via return or via the callback function), it must
return a stream.
For instance, you can decompress the request body:
fastify.addHook('preParsing', (request, reply, payload, done) => {
// Some code
done(null, newPayload)
})
Or async/await:
fastify.addHook('preParsing', async (request, reply, payload) => {
// Some code
await asyncMethod()
return newPayload
})
Notice: in the preParsing hook, request.body will always be
undefined, because the body parsing happens before the
preValidation hook.
Notice: you should also add a receivedEncodedLength property to the
returned stream. This property is used to correctly match the request payload
with the Content-Length header value. Ideally, this property should be updated
on each received chunk.
Notice: The size of the returned stream is checked to not exceed the limit
set in bodyLimit option.
preValidation
If you are using the preValidation hook, you can change the payload before it
is validated. For example:
fastify.addHook('preValidation', (request, reply, done) => {
request.body = { ...request.body, importantKey: 'randomString' }
done()
})
Or async/await:
fastify.addHook('preValidation', async (request, reply) => {
const importantKey = await generateRandomString()
request.body = { ...request.body, importantKey }
})
preHandler
The preHandler hook allows you to specify a function that is executed before
a routes's handler.
fastify.addHook('preHandler', (request, reply, done) => {
// some code
done()
})
Or async/await:
fastify.addHook('preHandler', async (request, reply) => {
// Some code
await asyncMethod()
})
preSerialization
If you are using the preSerialization hook, you can change (or replace) the
payload before it is serialized. For example:
fastify.addHook('preSerialization', (request, reply, payload, done) => {
const err = null
const newPayload = { wrapped: payload }
done(err, newPayload)
})
Or async/await:
fastify.addHook('preSerialization', async (request, reply, payload) => {
return { wrapped: payload }
})
Note: the hook is NOT called if the payload is a string, a Buffer, a
stream, or null.
onError
fastify.addHook('onError', (request, reply, error, done) => {
// Some code
done()
})
Or async/await:
fastify.addHook('onError', async (request, reply, error) => {
// Useful for custom error logging
// You should not use this hook to update the error
})
This hook is useful if you need to do some custom error logging or add some specific header in case of error.
It is not intended for changing the error, and calling reply.send will throw
an exception.
This hook will be executed only after
the Custom Error Handler set by setErrorHandler
has been executed, and only if the custom error handler sends an error back to the
user
(Note that the default error handler always sends the error back to the
user).
Notice: unlike the other hooks, passing an error to the done function is not
supported.
onSend
If you are using the onSend hook, you can change the payload. For example:
fastify.addHook('onSend', (request, reply, payload, done) => {
const err = null;
const newPayload = payload.replace('some-text', 'some-new-text')
done(err, newPayload)
})
Or async/await:
fastify.addHook('onSend', async (request, reply, payload) => {
const newPayload = payload.replace('some-text', 'some-new-text')
return newPayload
})
You can also clear the payload to send a response with an empty body by
replacing the payload with null:
fastify.addHook('onSend', (request, reply, payload, done) => {
reply.code(304)
const newPayload = null
done(null, newPayload)
})
You can also send an empty body by replacing the payload with the empty string
'', but be aware that this will cause theContent-Lengthheader to be set to0, whereas theContent-Lengthheader will not be set if the payload isnull.
Note: If you change the payload, you may only change it to a string, a
Buffer, a stream, a ReadableStream, a Response, or null.
onResponse
fastify.addHook('onResponse', (request, reply, done) => {
// Some code
done()
})
Or async/await:
fastify.addHook('onResponse', async (request, reply) => {
// Some code
await asyncMethod()
})
The onResponse hook is executed when a response has been sent, so you will not
be able to send more data to the client. It can however be useful for sending
data to external services, for example, to gather statistics.
Note: setting disableRequestLogging to true will disable any error log
inside the onResponse hook. In this case use try - catch to log errors.
onTimeout
fastify.addHook('onTimeout', (request, reply, done) => {
// Some code
done()
})
Or async/await:
fastify.addHook('onTimeout', async (request, reply) => {
// Some code
await asyncMethod()
})
onTimeout is useful if you need to monitor the request timed out in your
service (if the connectionTimeout property is set on the Fastify instance).
The onTimeout hook is executed when a request is timed out and the HTTP socket
has been hung up. Therefore, you will not be able to send data to the client.
onRequestAbort
fastify.addHook('onRequestAbort', (request, done) => {
// Some code
done()
})
Or async/await:
fastify.addHook('onRequestAbort', async (request) => {
// Some code
await asyncMethod()
})
The onRequestAbort hook is executed when a client closes the connection before
the entire request has been processed. Therefore, you will not be able to send
data to the client.
Notice: client abort detection is not completely reliable. See: Detecting-When-Clients-Abort.md
Manage Errors from a hook
If you get an error during the execution of your hook, just pass it to done()
and Fastify will automatically close the request and send the appropriate error
code to the user.
fastify.addHook('onRequest', (request, reply, done) => {
done(new Error('Some error'))
})
If you want to pass a custom error code to the user, just use reply.code():
fastify.addHook('preHandler', (request, reply, done) => {
reply.code(400)
done(new Error('Some error'))
})
The error will be handled by Reply.
Or if you're using async/await you can just throw an error:
fastify.addHook('onRequest', async (request, reply) => {
throw new Error('Some error')
})