What is Fastify?
It was born in late 2016, and since its first release in 2018, it has grown at the speed of light. It joined the OpenJS Foundation as an At-Large project in 2020 when it reached version 3, which is the version we are going to work with!
This framework focuses on unique aspects that are uncommon in most other web frameworks:
- Improvement of the developer experience: This streamlines their work and promotes a plugin design system. This architecture helps you structure your application in smaller pieces of code and apply good programming patterns such as DRY (Don’t Repeat Yourself), Immutability, and Divide & Conquer.
- Comprehensive performance: This framework is built to be the fastest.
- Up to date with the evolution of the Node.js runtime: This includes quick bugfixes and feature delivery.
- Ready to use: Fastify helps you set up the most common issues you may face during the implementation, such as application logging, security concerns, automatic test implementation, and user input parsing.
- Community-driven: Supports and listens to the framework users.
The result is a flexible and highly extensible framework that will lead you to create reusable components. These concepts give you the boost to develop a proof of concept (PoC) or large applications faster and faster. Creating your plugin system takes less time to meet the business need without losing the possibility to create an excellent code base and a performant application.
Fastify provides all these aspects to you through a small set of components that we are going to look at in the next section.
Fastify makes it easier to create and manage an HTTP server and the HTTP request lifecycle, hiding the complexity of the Node.js standard modules. It has two types of components: the main components and utility elements. The former comprise the framework, and it is mandatory to deal with them to create an application. The latter includes all the features you may use at your convenience to improve the code reusability.
- The root application instance represents the Fastify API at your disposal. It manages and controls the standard Node.js
http.Serverclass and sets all the endpoints and the default behavior for every request and response.
- A plugin instance is a child object of the application instance, which shares the same interface. It isolates itself from other sibling plugins to let you build independent components that can’t modify other contexts. Chapter 2 explores this component in depth, but we will see some examples here too.
Requestobject is a wrapper of the standard Node.js
http.IncomingMessagethat is created for every client’s call. It eases access to the user input and adds functional capabilities, such as logging and client metadata.
Replyobject is a wrapper of the standard Node.js
http.ServerResponseand facilitates sending a response back to the user.
The utility components, which will be further discussed in Chapter 4 are:
- The hook functions that act, when needed, during the lifecycle of the application or a single request and response
- The decorators, which let you augment the features installed by default on the main components, avoiding code duplication
- The parsers, which are responsible for the request’s payload conversion to a primitive type
That’s all! All these parts work together to provide you with a toolkit that supports you during every step of your application lifetime, from prototyping to testing, without forgetting the evolution of your code base to a manageable one.
Many names for one component
It is essential to learn the component’s name, especially the plugin instance one. It has many synonyms, and the most common are plugin, instance, or child instance. The Fastify official documentation uses these terms broadly and interchangeably, so it is beneficial to keep them all in mind.
We have read about all the actors that build Fastify’s framework and implement its focus aspects. Thanks to this quick introduction, you know their names, and we will use them in the following sections. The following chapters will further discuss every component and unveil their secrets.