How to Build a Blazingly Fast CRUD API with Fastify

How to Build a Blazingly Fast CRUD API with Fastify

Learn how to build a fast and simple CRUD API using Fastify, while also implementing data validation and adding plugins. Fastify is a framework for backend web development with a powerful plugin architecture and the least overhead. It’s primarily inspired by Hapi and Express and is one of the fastest web frameworks running on Node.js.

Learn how to build a fast and simple CRUD API using Fastify, while also implementing data validation and adding plugins.

Fastify is a framework for backend web development with a powerful plugin architecture and the least overhead. It’s primarily inspired by Hapi and Express and is one of the fastest web frameworks running on Node.js.

Fastify v3.0 is the latest version and has been available since early July 2020. Version 3 has some exceptional new functionalities such as:

  • Running an Express application inside Fastify
  • Changes in logging serialization
  • Changes in schema substitution
  • Better TypeScript support

An overview of Fastify’s features

Now that we’ve covered Fastify’s latest version update functionalities, let’s review a shortlist of Fastify’s most important features:

High performance

Depending on code complexity, it can serve around 30,000 requests per second or more for less complex business logic

Extendibility

Hooks, plugins, and decorators make it fully extensible

Scheme-based

Internally, Fastify compiles a JSON schema to a highly performant function that can be used for route validation and output serialization

Logging

Pino is a highly cost-effective logger used for faster logging

Developer-friendly environment

The framework is expressive and easy to get started with. Furthermore, it allows developers to quickly scale small projects to larger performant applications without sacrificing performance or security

TypeScript-ready

The TypeScript type declaration file maintains support to the TypeScript community

5 important Fastify plugins you should know

Besides the vast amount of features that Fastify offers, it also has a strong plugin architecture. Any developer can build plugins that work with Fastify to create quick building blocks for bootstrapping your API project.

Plugins developed by external developers fall under the category of “community plugins,” while the Fastify team also maintains some plugins of their own, which they label as “core plugins.” However, it’s important to know that any community plugin must adhere to Fastify’s best practices.

The benefit of using core plugins is that the Fastify team actively maintains those plugins, but keep in mind that community plugins may not be maintained.

Here are some important Fastify plugins to note:

  • fastify-auth: An authentication plugin developed by the Fastify team that allows you to quickly inject authentication logic to your API routes
  • fastify-cors: Cross-origin requests are important for any application, and fastify-cors helps you manage this without the need for installing a CORS package separately
  • fastify-jwt: This plugin will decorate your application with standard JSON web token. Fastify-jwt internally uses the jsonwebtoken package
  • fastify-nextjs: Next is a React framework for building pre-rendering websites at the server-side. This plugin helps you do the same with Fastify
  • fastify-redis: This enables your Fastify application to share the same Redis connection throughout the server

And that’s not even a comprehensive list. Fastify has a wide range of plugins to choose from.

Fastify vs Koa vs Express

Each framework, of course, has its pros and cons — but each framework also has its application. It’s not easy to compare diverse frameworks. However, I tried to select relevant evaluation criteria for when you are choosing a framework.

Speed comparison

Here’s an overview of the speed comparison by StackShare.io.

Express: Express processes the least amount of request/sec. The benchmark proves that Express can handle 15,978 requests/sec

Koa: Koa is a better option than Express. It’s also a lightweight framework that handles 54,848 requests/sec

Fastify: Fastify received the best benchmark results with 78,956 requests/sec

Plugins ecosystem

As discussed in the previous section, Fastify is the only web framework of these three that has a wide range of plugins, and it makes a major difference. It’s a big plus for developers because they don’t have to rely on multiple frameworks or packages to build their applications. Fastify becomes the one-stop solution.

TypeScript support

Again, Fastify is the only framework that has TypeScript support out of the box. Depending on your Node.js version, you may need to install @types/node.

Creating your first server with Fastify 3.0

And now, the exciting part! This tutorial will guide you through building your first server with Fastify, covering the following aspects:

  1. Installation
  2. Run your first server
  3. Add routes to your API
  4. Use Fastify plugins
  5. Add data validation capabilities

Ready? Let’s begin.

fastify api node javascript developer

Bootstrap 5 Complete Course with Examples

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Building a simple Applications with Vue 3

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

A Simple Guide to API Development Tools

APIs can be as simple as 1 endpoint for use by 100s of users or as complex as the AWS APIs with 1000s of endpoints and 100s of thousands of users. Building them can mean spending a couple of hours using a low-code platform or months of work using a multitude of tools. Hosting them can be as simple as using one platform that does everything we need or as complex as setting up and managing ingress control, security, caching, failover, metrics, scaling.

Tracking a Developer’s Journey From Documentation Visit

Measuring website activity provides only half the story. See how to best track the developer's journey and what funnel stages makes sense for API-first products

How to Market to Developers with Paid Marketing

Selling to developers is hard. How to market to developers efficiently using paid advertising leveraging inbound marketing techniques.

54% of Developers Cite Lack of Documentation as the Top Obstacle to Consuming APIs

APIs are perceived as reliable—more than half of respondents stated that APIs do not break, stop working, or materially change specification often enough to matter.

Top Node.js Development Companies and Expert NodeJS Developers

A thoroughly researched list of top NodeJS development companies with ratings & reviews to help hire the best Node.JS developers who provide development services and solutions across the world. List of Leading Node.js development Service Providers...