Guide
How to Migrate from Express to Fastify 2026




Migrate an Express API to Fastify with route examples, middleware mapping, plugins, TypeScript types, and a before-and-after performance check.
Contents
Contents
TL;DR
Migrate one route first. Translate its handler to Fastify's request and reply APIs, add a request schema if it needs validation, and compare the status, headers and body with the Express route. Inventory the middleware that route relies on before moving any shared hooks.
Key Takeaways
- Register native Fastify plugins and use hooks for work that runs around a route handler.
- Use JSON Schema for built-in request validation where it fits.
- Use Fastify route generics to type request data and replies.
- Use
@fastify/expressas a temporary bridge for Express middleware. - Compare equivalent routes under the same load before counting on a throughput gain.
Before You Start: Assess Your Express Setup
Before touching a line of code, map out what your Express application is actually doing. The migration complexity is determined almost entirely by your middleware stack, not your route handlers.
Walk through your app.ts or server.ts and inventory: every app.use() call, every custom error handler, every third-party middleware package, and any patterns where middleware attaches custom properties to req. Most route handlers migrate in minutes — a complex middleware chain that threads state through multiple middleware functions takes more thought.
Make a list like this before starting:
- Global middleware: cors, helmet, body parsing, compression, morgan
- Auth middleware: passport, JWT verification, session handling
- Custom middleware: request ID generation, tenant detection, rate limiting
- Error handlers: the
(err, req, res, next)4-argument handlers - Static file serving:
express.static()
For each item, you need a Fastify equivalent. Most have direct counterparts in the @fastify/* plugin ecosystem. The few that don't can usually be bridged using @fastify/express during a gradual migration.
Step 1: Install Fastify and Create the Server
Start by installing Fastify and the plugin packages you'll need:
npm install fastify
npm install -D @types/node # If TypeScript
# Common Fastify plugins (replacing Express middleware):
npm install @fastify/cors # cors
npm install @fastify/helmet # helmet
npm install @fastify/rate-limit # express-rate-limit
npm install @fastify/jwt # jsonwebtoken wrapper
npm install @fastify/cookie # cookie-parser
npm install @fastify/static # express.static
npm install @fastify/multipart # multer (file uploads)
npm install @fastify/sensible # HTTP error helpers
Create the Fastify server alongside your existing Express server. Don't delete Express yet — run both on different ports during the migration:
// src/server.ts — new Fastify server
import Fastify from 'fastify';
const fastify = Fastify({
logger: true, // Built-in Pino logger (replaces Morgan)
});
// Register plugins
await fastify.register(import('@fastify/cors'), {
origin: process.env.CORS_ORIGIN ?? '*',
credentials: true,
});
await fastify.register(import('@fastify/helmet'));
await fastify.register(import('@fastify/sensible'));
// Start server
await fastify.listen({ port: 3001, host: '0.0.0.0' });
console.log('Fastify listening on port 3001');
Compare the Express app creation to Fastify's:
// Express (before):
import express from 'express';
import cors from 'cors';
import helmet from 'helmet';
const app = express();
app.use(express.json());
app.use(cors());
app.use(helmet());
app.listen(3000, () => console.log('Express listening on port 3000'));
// Fastify (after):
import Fastify from 'fastify';
const fastify = Fastify({ logger: true }); // JSON body parsing is built-in
await fastify.register(import('@fastify/cors'));
await fastify.register(import('@fastify/helmet'));
await fastify.listen({ port: 3000, host: '0.0.0.0' });
Note that express.json() has no equivalent in Fastify because Fastify parses JSON request bodies automatically. You don't need to register body parsing middleware.
Step 2: Migrate Routes
Route migration is the most mechanical part. The HTTP method names are identical (get, post, put, delete, patch). The path syntax is the same. The async handler pattern works the same. The main differences are: use request and reply instead of req and res, use reply.send() instead of res.json(), use reply.code() instead of res.status(), and you can return a value directly instead of calling res.json().
// Express route syntax:
app.get('/users/:id', async (req, res) => {
const { id } = req.params;
const user = await db.user.findById(id);
if (!user) return res.status(404).json({ error: 'Not found' });
res.json(user);
});
app.post('/users', async (req, res) => {
const { name, email } = req.body;
const user = await db.user.create({ name, email });
res.status(201).json(user);
});
// Fastify equivalent:
fastify.get('/users/:id', async (request, reply) => {
const { id } = request.params as { id: string };
const user = await db.user.findById(id);
if (!user) return reply.code(404).send({ error: 'Not found' });
return user; // Just return — Fastify auto-serializes
});
fastify.post('/users', async (request, reply) => {
const { name, email } = request.body as { name: string; email: string };
const user = await db.user.create({ name, email });
return reply.code(201).send(user);
});
The return user pattern (returning the response value directly instead of calling reply.send()) is idiomatic Fastify. The framework intercepts the return value and serializes it. This makes handler code cleaner, especially for simple GET routes.
Step 3: TypeScript Typed Routes
This is where the migration pays its biggest dividend. Express's TypeScript types are famously imprecise — req.body is typed as any, req.params is Record<string, string>, and getting proper types requires manual casting everywhere. Fastify's generic-based type system gives you precise types throughout the handler:
// Fastify generics give you full type safety:
interface CreateUserBody {
name: string;
email: string;
}
interface UserParams {
id: string;
}
interface UserResponse {
id: string;
name: string;
email: string;
createdAt: string;
}
// RouteGenericInterface — typed request and response
fastify.post<{
Body: CreateUserBody;
Reply: UserResponse;
}>('/users', async (request, reply) => {
const { name, email } = request.body;
// TypeScript knows: { name: string; email: string }
const user = await db.user.create({ name, email });
return reply.code(201).send(user);
// TypeScript validates: must match UserResponse shape
});
// Typed GET with params and query string:
fastify.get<{
Params: { id: string };
Querystring: { include?: string };
Reply: UserResponse;
}>('/users/:id', async (request, reply) => {
const { id } = request.params; // string
const { include } = request.query; // string | undefined
const user = await db.user.findById(id);
if (!user) return reply.notFound(); // @fastify/sensible
return user;
});
Step 4: Migrate Middleware to Plugins
Fastify's plugin system is the biggest conceptual shift. In Express, app.use() adds middleware that runs for all subsequent routes. In Fastify, you register plugins that encapsulate functionality and can be scoped to specific route groups.
// Express middleware pattern:
app.use(cors());
app.use(helmet());
app.use(express.json());
// Fastify plugin pattern:
const fastify = Fastify({ logger: true });
// Register global plugins before routes
await fastify.register(import('@fastify/cors'), {
origin: process.env.CORS_ORIGIN,
credentials: true,
});
await fastify.register(import('@fastify/helmet'));
await fastify.register(import('@fastify/sensible'));
// Route-scoped plugins (only applies to routes in this scope)
await fastify.register(async (scope) => {
await scope.register(import('@fastify/jwt'), {
secret: process.env.JWT_SECRET!,
});
// These routes require JWT auth:
scope.addHook('preHandler', async (request) => {
await request.jwtVerify();
});
scope.get('/me', async (request) => {
return request.user;
});
scope.get('/dashboard', async (request) => {
return { data: await getDashboardData(request.user.id) };
});
}, { prefix: '/api/v1' });
This hook applies inside the registered scope. Test every protected route with and without credentials, especially after moving a route to a different plugin.
Common Express-to-Fastify middleware mappings:
| Express Package | Fastify Equivalent |
|---|---|
cors | @fastify/cors |
helmet | @fastify/helmet |
express-rate-limit | @fastify/rate-limit |
cookie-parser | @fastify/cookie |
multer | @fastify/multipart |
express.static | @fastify/static |
morgan | Built-in Pino logger |
jsonwebtoken + middleware | @fastify/jwt |
compression | @fastify/compress |
Step 5: Hooks — Replacing Middleware Logic
Fastify's hook system replaces the common pattern of Express middleware that modifies req or performs pre-route actions. Hooks run at specific lifecycle points and have direct access to request and reply objects:
// Express: use middleware for cross-cutting concerns
app.use((req, res, next) => {
req.requestId = uuid();
next();
});
// Fastify: use hooks
fastify.addHook('onRequest', async (request, reply) => {
request.requestId = crypto.randomUUID();
});
// Fastify hook lifecycle (in execution order):
// 1. onRequest — first, before parsing
// 2. preParsing — before body parsing
// 3. preValidation — before schema validation
// 4. preHandler — before route handler (auth, guards)
// 5. handler — your route function
// 6. preSerialization — before response serialization
// 7. onSend — before sending response
// 8. onResponse — after response sent (logging, cleanup)
// 9. onError — on unhandled error
Hooks can be global (registered on the root fastify instance) or scoped (registered inside a register callback, only applying to routes in that scope). This gives you more control than Express's linear middleware chain.
Step 6: Error Handling
Express's error middleware is identified by its 4-argument signature (err, req, res, next). Fastify uses a dedicated setErrorHandler method:
// Express error middleware:
app.use((err: Error, req: Request, res: Response, next: NextFunction) => {
console.error(err.stack);
res.status(500).json({ error: 'Internal Server Error' });
});
// Fastify error handler:
fastify.setErrorHandler(async (error, request, reply) => {
fastify.log.error(error);
// Validation errors (from JSON Schema)
if (error.validation) {
return reply.code(400).send({
error: 'Validation Error',
details: error.validation,
});
}
if (error.statusCode && error.statusCode >= 400 && error.statusCode < 500) {
return reply.code(error.statusCode).send({ error: error.message });
}
return reply.code(500).send({ error: 'Internal Server Error' });
});
// In route handlers — throw to trigger the error handler:
fastify.get('/users/:id', async (request, reply) => {
const user = await db.user.findById(request.params.id);
if (!user) throw fastify.httpErrors.notFound('User not found'); // @fastify/sensible
return user;
});
Use fastify.httpErrors to create an error you intend to throw. The reply helpers send the error response.
Step 7: JSON Schema Validation
Express has no built-in request validation — teams typically reach for Joi, Zod, or manual validation. Fastify has JSON Schema validation built into every route:
// Fastify validates the request and serializes the reply using these schemas
fastify.post('/users', {
schema: {
body: {
type: 'object',
required: ['name', 'email'],
properties: {
name: { type: 'string', minLength: 1, maxLength: 50 },
email: { type: 'string', format: 'email' },
},
},
response: {
201: {
type: 'object',
properties: {
id: { type: 'string' },
name: { type: 'string' },
email: { type: 'string' },
},
},
},
},
}, async (request, reply) => {
// request.body is validated — name and email guaranteed to exist
const user = await db.user.create(request.body);
return reply.code(201).send(user);
});
Fastify validates the request and uses the response schema for serialization. Include every field the client needs and compare the serialized response with the Express version. See Fastify's validation and serialization reference.
If you prefer Zod over JSON Schema, the fastify-type-provider-zod package integrates them cleanly:
import { serializerCompiler, validatorCompiler } from 'fastify-type-provider-zod';
fastify.setValidatorCompiler(validatorCompiler);
fastify.setSerializerCompiler(serializerCompiler);
Gradual Migration: Run Both
The @fastify/express compatibility table maps plugin versions to Fastify versions. The example uses Fastify v5 and plugin v4. Register the bridge before calling fastify.use(), then move routes to native Fastify handlers. An illustrative middleware bridge looks like this.
import fastifyExpress from '@fastify/express';
await fastify.register(fastifyExpress);
fastify.use(legacyExpressMiddleware());
Check that a bridged request still gets the same headers, status and body. Exercise a native Fastify route in the same test so you can see whether the middleware affects both. Remove the bridge when the last dependent route or middleware has moved.
Measure Performance Before and After
Pick a route with representative traffic and compare the Express and Fastify versions with the same request payload, response schema and dependencies. Keep the Node.js version, concurrency, warm-up, database state and logging settings consistent. Record successful requests per second, median and tail latency, and error rate. Recheck the returned payload so a faster run cannot hide dropped fields or requests.
Fastify's benchmarking guide shows how to build a workload. An isolated framework test measures framework overhead. A route that waits on a database or external service may give a different result.
Package Capabilities
| Package | TypeScript | Request validation |
|---|---|---|
fastify | Built-in route generics | Built-in JSON Schema validation |
express | Via @types/express | Add validation in middleware or a handler |
Track download counts on the package pages. Downloads count npm activity, so use your own route tests to decide whether migrating is worthwhile.
Common Migration Pitfalls
A few issues come up repeatedly during Express-to-Fastify migrations. Being aware of them upfront saves debugging time.
Forgetting await on fastify.listen(): Express's app.listen() is synchronous. Fastify's is async. Forgetting await means your plugins may not be registered before the server starts accepting connections, leading to "route not found" errors on the first few requests.
Plugin registration order: Fastify registers plugins asynchronously and in order. A route that depends on a plugin (like @fastify/jwt) must be registered after that plugin. If you see "fastify.authenticate is not a function," check that the JWT plugin is registered before the route handler tries to use it.
request.params type casting: In Express with TypeScript, req.params is typed as Record<string, string>, and you access params with req.params.id. The same works in Fastify, but you may need to add the generic type or cast to avoid TypeScript errors: const { id } = request.params as { id: string }. Using typed route generics (fastify.get<{ Params: { id: string } }>) eliminates the cast entirely.
Reply already sent: In an async handler, return the value or return reply.send(value). After sending an early error response, return so the rest of the handler cannot send another response.
Express next() doesn't exist: If you're copying Express middleware logic, remember there's no next() in Fastify hooks. To move to the next step, simply don't call anything — Fastify's lifecycle continues automatically. To abort the request with an error, throw an error or call reply.send() with an error response.
Is the Migration Worth It?
Start with a route whose middleware and response contract you can verify. Continue if Fastify's schemas or scoped plugins make the route easier to maintain, or if the before-and-after test shows a gain that matters to your application.
Related Guides
Dig deeper: compare both frameworks head-to-head on the Express vs Fastify comparison page, read the Express vs Hono comparison for 2026, or check the download trends and changelog on the Fastify package page.