Home > English > Initial thoughts using Fastify

Initial thoughts using Fastify

If you are a full-stack developer than the decision as to which back-end server library to use has crossed your path multiple times. If you are a NodeJS developer the choices are little more narrow but you still have a lot of overwhelming choices. Probably the most popular are Express and Koa but there is a new kid on the block that is truly amazing.

The creators of Fastify had one initial goal in mind, building the fastest server for NodeJS developers. When they looked at the existing open sourced frameworks, they discovered some pain points that could be corrected but that would introduce breaking changes to existing application programming interfaces (API). This led to the development of Fastify.

What is Fastify?

Fastify is a web framework highly focused on providing the best developer experience with the least overhead and powerful plugin architecture. It is also considered one of the fastest frameworks out there. So how does it do this? Well, the simply answer is schema. In JavaScript, the process of serializing and deserializing objects is very expensive and inefficient. The authors of Fastify recognized this and decided to take this problem on head-on. They came up with their own serialization engine that used schema to speed up the process. By using typings provided in a schema, Fastify can optimize the serialization process for any given operation. This includes both incoming requests and outgoing responses.

Fastify is also fully extendible via its hooks, plugins, and decorators.

What are Plugins?

As you write your server code, you always try to follow the don’t repeat yourself (DRY) principle and keep your code clean but it can get messy quick. In a typical Express server, you will find that you are requiring multiple packages as middleware to help configure security, parsing, database configurations, etc. If you were to look at your server.js file after you have completed a vanilla server implementation, you would a healthy spread of custom code and middleware configurations.

Fastify tries to help you keep your code as clean as possible by introducing a Plugin model. In fact, you can even take it a step further by separating your development concerns by placing all of your third-party dependency configuration in a plugins folder and all of your implementation code in a services folder. This really cleans up your code and it also facilitates migrations from one cloud provider to another without a lot of provider specific code to change. By separating everything out, you can now have cloud provider specific plugins that will just work. If you want to try a different cloud provider, simply swap out that plugin for another cloud provider.

Fastify currently has 36 core plugins and 75 community plugins. But the story doesn’t end there, Fastify has a simple API that allows you to author your own plugin if you can’t find exactly what you are looking for.

If you wanted to ensure your endpoints enable CORS, you can use the fastify-cors plugin. If you wanted to support Web Sockets, you simply grab the fastify-websocket plugin. You want Swagger documentation, use the fastify-swagger plugin.

Fastify has a register function that allow you to bring in your plugin. This function takes two parameters, one for the actual plugin, which is typically resolved using require, and the other for configuration to provide to the plugin.

What I really like about this architecture is that you can use an auto loader that will scan your plugins and services folders and automatically bring in all of your dependencies. This removes so much ceremony and manual coding by using a simple convention which is something I am a big fan of.

So why should I care?

I like build robust systems that have very little redundancy. For example, if I needed to build a REST API, I would create a generic contract and have a single dynamic endpoint to handle each of the create, retrieve, update, and delete operations. This reduces the need to have a 1:1 implementation across all of your tables or collections. It requires a little more design up front but the amount of code to maintain as well testing is tiny compared to the traditional verbose and redundant approach. Fastify helps with separation of concerns as well as building complex servers while still keeping things as simple as possible.

What’s next?

Take some time and play with Fastify. You may find that it resonates well with you. This was a very high-level introduction to Fastify and doesn’t even scratch the surface of all the capabilities you can use. I am very happy with the library! In a future post, we will take a look at a reference implementation that I use for my clients.

Categories: English Tags: ,
  1. No comments yet.
  1. No trackbacks yet.

Leave a comment