Fastify and its plugins are great

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.

mkdir awesomer
cd awesomer
npm init -y
npm install --save fastify

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

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.

npm i --save-dev nodemon

Run the server!

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

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

    module.exports = async function(fastify, opts) {
      fastify.register(require("fastify-axios"));
    };
    
  3. Start using Axios from anywhere in the server.

    // 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!

comments powered by Disqus