Understanding the request and reply lifecycle
When executing a Fastify server application, the vast majority of the time is spent in the request-reply cycle. As developers, we define routes that the clients will call and produce a response based on the incoming conditions. In true Fastify philosophy, we have several events at our disposal to interact with this cycle. As usual, they will be triggered automatically by the framework only when needed. These hooks are fully encapsulated so that we can control their execution context with the register
method.
As we saw in the previous section, we had four application hooks. Here, we have nine request and reply hooks:
onRequest
preParsing
preValidation
preHandler
preSerialization
onSend
onResponse
onError
onTimeout
Since they are part of the request/reply cycle, the trigger order of these events is crucial. Therefore, the first seven elements of the list are written from the first to the last...