This page looks best with JavaScript enabled

Fastify and its plugins are great

 ·   ·  ☕ 3 min read

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-

  1. 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
  2. Simplicity - just like Express, Fastify is simple and elegant
  3. 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.
  4. 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.

1
2
3
4
mkdir awesomer
cd awesomer
npm init -y
npm install --save fastify

Open directory in VSCode. Create a new file called server.js.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
const fastify = require("fastify")();

fastify.get("/", async (request, reply) => {
  return { message: "hello world" };
});

// Run!
const start = async () => {
  try {
    await fastify.listen(3000);
    fastify.log.info(`server listening on ${fastify.server.address().port}`);
  } catch (err) {
    fastify.log.error(err);
    process.exit(1);
  }
};
start();

Use something like nodemon to respond your changes in development.

1
npm i --save-dev nodemon

Run the server!

1
nodemon 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..

  1. Install Axios Fastify Plugin

    1
    
    npm install --save fastify-axios
    
  2. Register plugin.

    1
    2
    3
    
    module.exports = async function(fastify, opts) {
      fastify.register(require("fastify-axios"));
    };
    
  3. 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!

Stay in touch!
Share on

Prashanth Krishnamurthy
WRITTEN BY
Prashanth Krishnamurthy
Technologist | Creator of Things