DEV Community

Alex Spinov
Alex Spinov

Posted on

Fastify Has a Free Node.js Framework That's 3x Faster Than Express — Schema Validation, Plugins, TypeScript

The Express Problem

Express was built in 2010. No built-in validation. No TypeScript support. No schema documentation. And it's slow — 15K requests/sec vs. Fastify's 60K+.

Fastify is Express's modern replacement. JSON Schema validation, plugin architecture, TypeScript-first, 3x faster.

What Fastify Gives You

Simple API

import Fastify from 'fastify';

const app = Fastify({ logger: true });

app.get('/', async () => {
  return { hello: 'world' };
});

app.listen({ port: 3000 });
Enter fullscreen mode Exit fullscreen mode

JSON Schema Validation (+ Auto Documentation)

app.post('/users', {
  schema: {
    body: {
      type: 'object',
      required: ['name', 'email'],
      properties: {
        name: { type: 'string', minLength: 1 },
        email: { type: 'string', format: 'email' },
        age: { type: 'integer', minimum: 0 },
      },
    },
    response: {
      200: {
        type: 'object',
        properties: {
          id: { type: 'integer' },
          name: { type: 'string' },
        },
      },
    },
  },
  handler: async (request) => {
    // request.body is validated and typed
    return { id: 1, ...request.body };
  },
});
Enter fullscreen mode Exit fullscreen mode

Schema validates input AND serializes output (faster JSON.stringify).

Plugin System

import cors from '@fastify/cors';
import jwt from '@fastify/jwt';
import swagger from '@fastify/swagger';

app.register(cors);
app.register(jwt, { secret: 'your-secret' });
app.register(swagger, { openapi: { info: { title: 'My API', version: '1.0.0' } } });
Enter fullscreen mode Exit fullscreen mode

Hooks (Lifecycle)

app.addHook('onRequest', async (request) => {
  // Authentication check
  const token = request.headers.authorization;
  if (!token) throw app.httpErrors.unauthorized();
});

app.addHook('onResponse', async (request, reply) => {
  // Logging, metrics
  console.log(`${request.method} ${request.url}${reply.statusCode}`);
});
Enter fullscreen mode Exit fullscreen mode

Performance

Framework Requests/sec
Express 15,000
Koa 25,000
Fastify 65,000

TypeScript Support

Full TypeScript support with generic types for request body, params, querystring, and headers.

Quick Start

npm install fastify
Enter fullscreen mode Exit fullscreen mode

Why This Matters

Express served us well for 15 years. Fastify is the next generation — faster, safer, and built for TypeScript.


Building high-performance APIs? Check out my web scraping actors on Apify Store — data at speed. For custom solutions, email spinov001@gmail.com.

Top comments (0)