How a boot sequence works in a Nodejs server and why you should care about it
Understand async boot and use it to prevent production errors

Short intro to Fastify
Fastify is a Nodejs framework developed with plugins as a core concept.
In Fastify, you can register plugins to add functionalities to your system, making your code reusable. To be safe and reliable, this plugin system needs a process ensuring that all the plugins are loaded in the correct order. To achieve this goal, Fastify uses a tool called Avvio.
This is the feature I will discuss in this post: the async boot of the Fastify server.
Why you need a boot sequence
In Node, when your server starts, there will be multiple operations.
Some of these operations will be, most likely, asynchronous. An example could be connecting to a database. In the following example, I created a simple express server that connects to a Mongo database and sets the “users” field of the request as the list of the users in the database.
Note that in the above example, there is a chance that the database is not connected yet when receiving a request to the “/users” route. To simulate this behavior, I added a timeout to the database connection so that you'll get an error if you run the server before “Database connected” is printed. In contrast, you'll get the response if you run it after that moment. While this is a basic example, such behavior might happen in a real-world scenario.
A system like this one is unreliable and might lead to unexpected errors in production.
Adding the async boot
To add the async boot to the previous example, we can use the same tool used by Fastify: Avvio.
As you can see, in the above example, we create the “app” passing the express server to Avvio. Next, you can see that all the logic (database connection and routes registrations) are encapsulated into functions we can pass directly to the Avvio app through the “use” method. Finally, we call the “ready” method before “server.listen”.
Avvio’s method “ready” ensures that all the functions registered using the “use” method are completed (called the “done” function) or, if they return a Promise, that all the promises are settled.
Thanks to Avvio, once our server starts listening, we are sure the client is connected to the database.
In the example you’ll notice that I used the option “expose” to rename the “use” method to “register” but I kept using “use”. Unfortunnately I experienced an error when trying to use “register” so I will investigate and update the post once fixed.
Using Fastify
The same example can be done directly using Fastify.
As you can see in the above example, we don’t need to use Avvio since it is integrated into Fastify itself. In Fastify, calling “listen” is equivalent to calling “ready” in Avvio.
Resources
This post was highly inspired by the book Accelerating Server-Side Development with Fastify by Manuel Spigolon, Maksim Sinik, and Matteo Collina. (affiliate link)
I highly recommend reading the book to dive deeper into this topic and Fastify.