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 });
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 };
},
});
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' } } });
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}`);
});
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
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)