Introduction

In the world of Node.js, Express has been a trusted companion for developers. However, as applications scale and require more performance, Fastify, a high-performance, low-overhead web framework for Node.js, has emerged as an excellent alternative. This blog post will guide you through migrating an existing Express application to Fastify, step by step.

The Express Application

Our Express application is a simple property management API with the following features:

  1. Middleware: Express middleware for logging and authorization.
  2. Routes: Three routes for creating, retrieving, and updating properties.
  3. Database: Uses LowDB, a JSON file-based database.
  4. Logging: Uses Winston with field redaction for sensitive data.

Here is the entire code for our Express application:

GitHub - maumercado/fastify-express-migration-poc: A simple example of how to migrate an express app to fastify

Let’s build it from scratch

You’ll need NodeJS installed in your system, 16 and onwards should work.

  1. First install the standard requirements:
$ npm install express express-async-handler [email protected] winston lodash-id
$ npm install -D standardjs supertest tap
  1. Let’s set the package.json:
{
  "name": "fastify-express-migration-poc",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "start": "node index.js",
    "lint": "npx standard",
    "lint:fix": "npx standard --fix",
    "test": "NODE_ENV=test tap test/**/*.test.js"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "dependencies": {
    "express": "^4.18.2",
    "express-async-handler": "^1.2.0",
    "lodash-id": "^0.14.1",
    "lowdb": "^1.0.0",
    "winston": "^3.8.2"
  },
  "devDependencies": {
    "standardjs": "^1.0.0-alpha",
    "supertest": "^6.3.3",
    "tap": "^16.3.4"
  }
}
  1. Every application has an entry point and this is ours, index.js