This schema shows the internal lifecycle of Fastify.
The right branch of each section shows the next phase of the lifecycle. The left branch shows the corresponding error code generated if the parent throws an error. All errors are automatically handled by Fastify.
Incoming Request
β
βββΆ Routing
β
βββΆ Instance Logger
β
4**/5** βββ΄ββΆ onRequest Hook
β
4**/5** βββ΄ββΆ preParsing Hook
β
4**/5** βββ΄ββΆ Parsing
β
4**/5** βββ΄ββΆ preValidation Hook
β
400 βββ΄ββΆ Validation
β
4**/5** βββ΄ββΆ preHandler Hook
β
4**/5** βββ΄ββΆ User Handler
β
βββΆ Reply
β
4**/5** βββ΄ββΆ preSerialization Hook
β
βββΆ onSend Hook
β
4**/5** βββ΄ββΆ Outgoing Response
β
βββΆ onResponse Hook
Before or during the User Handler, reply.hijack() can be called to:
- Prevent Fastify from running subsequent hooks and the user handler
- Prevent Fastify from sending the response automatically
If reply.raw is used to send a response, onResponse hooks will still
be executed.
When the user handles the request, the result may be:
- In an async handler: it returns a payload or throws an
Error - In a sync handler: it sends a payload or an
Errorinstance
If the reply was hijacked, all subsequent steps are skipped. Otherwise, when submitted, the data flow is as follows:
β
schema validation Error
β
βββΆ schemaErrorFormatter
β
reply sent βββ JSON ββ΄β Error instance
β
β β
throw an Error
β
send or return β β
β β β
β βΌ β
reply sent βββ JSON ββ΄β Error instance βββΆ setErrorHandler βββββββ
β
reply sent βββ JSON ββ΄β Error instance βββΆ onError Hook
β
βββΆ reply sent
reply sent means the JSON payload will be serialized by one of the following:
- The reply serializer if set
- The serializer compiler if a JSON schema is set for the HTTP status code
- The default
JSON.stringifyfunction