Add a content type parser
Hook function that is called when creating a child logger instance for each request which allows for modifying or adding child logger bindings and logger options, or returning a completely custom child logger implementation.
Fastify default plain text parser
Fastify default error handler
Fastify default JSON parser
Frozen read-only object registering the initial options passed down by the user to the fastify instance
Optional allowOptional bodyOptional caseOptional connectionOptional disableOptional forceOptional http2?: booleanOptional http2Optional https?: boolean | Readonly<{ Optional ignoreOptional ignoreOptional keepOptional maxOptional onOptional onOptional pluginOptional requestOptional requestOptional useRemove all content type parsers, including the default ones
Remove an existing content type parser
Fastify schema serializer for all routes.
Fastify schema validator for all routes.
onRequest is the first hook to be executed in the request lifecycle. There was no previous hook, the next hook will be preParsing.
Notice: in the onRequest hook, request.body will always be null, because the body parsing happens before the preHandler hook.
preParsing is the second hook to be executed in the request lifecycle. The previous hook was onRequest, the next hook will be preValidation.
Notice: in the preParsing hook, request.body will always be null, because the body parsing happens before the preHandler hook.
preValidation is the third hook to be executed in the request lifecycle. The previous hook was preParsing, the next hook will be preHandler.
preHandler is the fourth hook to be executed in the request lifecycle. The previous hook was preValidation, the next hook will be preSerialization.
preSerialization is the fifth hook to be executed in the request lifecycle. The previous hook was preHandler, the next hook will be onSend.
Note: the hook is NOT called if the payload is a string, a Buffer, a stream or null.
You can change the payload with the onSend hook. It is the sixth hook to be executed in the request lifecycle. The previous hook was preSerialization, the next hook will be onResponse.
Note: If you change the payload, you may only change it to a string, a Buffer, a stream, or null.
onResponse is the seventh and last hook in the request hook lifecycle. The previous hook was onSend, there is no next hook.
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.
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 hanged up. Therefore you will not be able to send data to the client.
onRequestAbort is useful if you need to monitor the if the client aborts the request (if the request.raw.aborted property is set to true).
The onRequestAbort hook is executed when a client closes the connection before the entire request has been received. Therefore, you will not be able to send data to the client.
Notice: client abort detection is not completely reliable. See: https://github.com/fastify/fastify/blob/main/docs/Guides/Detecting-When-Clients-Abort.md
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 customErrorHandler has been executed, and only if the customErrorHandler sends an error back to the user (Note that the default customErrorHandler always sends the error back to the user). Notice: unlike the other hooks, pass an error to the done function is not supported.
Triggered when a new route is registered. Listeners are passed a routeOptions object as the sole parameter. The interface is synchronous, and, as such, the listener does not get passed a callback
Triggered when a new plugin is registered and a new encapsulation context is created. The hook will be executed before the registered code. This hook can be useful if you are developing a plugin that needs to know when a plugin context is formed, and you want to operate in that specific context. Note: This hook will not be called if a plugin is wrapped inside fastify-plugin.
Triggered when fastify.listen() or fastify.ready() is invoked to start the server. It is useful when plugins need a "ready" event, for example to load data before the server start listening for requests.
Triggered when fastify.listen() is invoked to start the server. It is useful when plugins need a "onListen" event, for example to run logics after the server start listening for requests.
Triggered when fastify.close() is invoked to stop the server. It is useful when plugins need a "shutdown" event, for example to close an open connection to a database.
Triggered when fastify.close() is invoked to stop the server. It is useful when plugins need to cancel some state to allow the server to close successfully.
Optional opts: FastifyListenOptionsVariadic listen method is deprecated. Please use .listen(optionsObject, callback) instead. The variadic signature will be removed in fastify@5
Variadic listen method is deprecated. Please use .listen(optionsObject, callback) instead. The variadic signature will be removed in fastify@5
Variadic listen method is deprecated. Please use .listen(optionsObject, callback) instead. The variadic signature will be removed in fastify@5
Optional address: stringOptional backlog: numberVariadic listen method is deprecated. Please use .listen(optionsObject) instead. The variadic signature will be removed in fastify@5
Prints the representation of the internal radix tree used by the router
Optional opts: PrintRoutesOptionsHook function that is called when creating a child logger instance for each request which allows for modifying or adding child logger bindings and logger options, or returning a completely custom child logger implementation.
Child logger bindings have a performance advantage over per-log bindings, because they are pre-serialised by Pino when the child logger is created.
For example:
function childLoggerFactory(logger, bindings, opts, rawReq) {
// Calculate additional bindings from the request
bindings.traceContext = rawReq.headers['x-cloud-trace-context']
return logger.child(bindings, opts);
}
Set a function that will be called whenever an error happens
Set a function that will generate a request-ids
Set the 404 handler
Optional preOptional preSet the reply serializer for all routes.
Set the schema controller for all routes.
Set the schema serializer for all routes.
Set the schema validator for all routes.
Fastify server instance. Returned by the core
fastify()method.