I had been meaning to use more of Fastify for work, but only made that a reality recently. So far I have had a great experience on the server framework.
What is Fastify?
Fastify is a low-overhead server framework built on top of NodeJS.
Fastify was started in late 2016 and jumped to v1.0 in Q1,2018. So - it’s a comparatively younger framework. But that has not stopped it from having great ambitions.
Fastify offers-
- High performance - low over-head and ludicrous speed. It can serve thousands of requests per second. It is ranked 115 coupled with Postgres DB and has very few competitors in Node world
- Simplicity - just like Express, Fastify is simple and elegant
- Productivity - has a great plugin ecosystem that can do almost everything. Use a plugin for custom hooks, decorators and more. Or, roll-out your own plugins.
- Developer friendly - great logging system, extensible using plugins, fast, can induce high productivity - what else can one ask for?
Personally and professionally, I had been jumping around the NodeJS server frameworks and a bit on ASP.NET Core. And, also wondering whether I should jump to serverless altogether (which I have not due to both cost and performance reasons).
Although I like Express, I have relied more on frameworks on that offer more - Adonis, Feathers and NestJS. So, I did not quite expect to take a liking to Fastify - which looks like yet another minimal server framework.
Fastify is great for me primarily due to its plugins - very much like how Express middleware calls me back to Express a few times a year. But, truth be told - I was initially drawn to Fastify because of the performance. The speed for both simple ‘hello world’ JSON response and more complex DB operations are great. Not that I worry about sub-second response times - I mean, my applications take a lot more than couple of seconds for many transactions.
Despite what my brain tells me about the performance impact - there is another part that continues to hum about the greatness arising out of the minimal speed boost.
Getting Started
Starting on Fastify is easy.
|
|
Open directory in VSCode. Create a new file called server.js
.
|
|
Use something like nodemon
to respond your changes in development.
|
|
Run the server!
|
|
Test the REST API using Insomnia and see the glorious message popping up.
Expore plugins
Plugins enhance what Fastify could do.
Let’s take a simple example of using Axios on our server to perform get
operations. To enable Axios, all you need to do is..
-
Install Axios Fastify Plugin
1
npm install --save fastify-axios
-
Register plugin.
1 2 3
module.exports = async function(fastify, opts) { fastify.register(require("fastify-axios")); };
-
Start using Axios from anywhere in the server.
1 2 3 4
// request via axios.get const { data, status } = await fastify.axios.get("https://google.com"); console.log("data", data); console.log("status", status);
And, you are done - up and running with Axios.
Finis
I have to use more of Fastify in real-world projects. At the same time, I don’t want that decision to be based on performance alone.
Onwards to the future!