Migrate Express.js REST APIs to Fastify with automated testing, performance benchmarking, and schema generation...
You MUST complete each phase IN ORDER. Do NOT proceed to the next phase until the current phase is complete. Report your progress after each phase.
Before writing ANY code, you MUST:
You MUST save the actual API responses BEFORE making any code changes. These are required to verify output compatibility in Phase 6.
For EACH endpoint identified in 1.1, capture the response:
# Example: save responses to files for later comparison
curl -s "http://localhost:PORT/endpoint" | jq . > /tmp/express_endpoint.json
curl -s "http://localhost:PORT/endpoint?query=test" | jq . > /tmp/express_endpoint_query.json
Save responses for:
You MUST benchmark the Express server BEFORE making any code changes. This baseline is required for performance comparison in Phase 6.
npm install autocannon --save-devnpx autocannon -c 10 -d 10 http://localhost:PORT/endpoint
STOP: You MUST report your assessment findings, saved API responses, AND Express baseline metrics before proceeding to Phase 2.
Install Fastify and equivalents for EACH Express plugin identified in Phase 1.
fastify - Core framework@fastify/helmet - Security headers (REQUIRED)@fastify/rate-limit - Rate limiting (REQUIRED)compression → @fastify/compresscors → @fastify/corscookie-parser → @fastify/cookieexpress-session → @fastify/sessionbody-parser → Built-in (no plugin needed)Keep Express installed until Phase 6 (Performance Verification) is complete. This allows you to run both servers simultaneously for benchmarking comparison.
STOP: Confirm all Fastify dependencies installed before Phase 3. Express stays installed for now.
For EACH route identified in Phase 1, you MUST:
Convert syntax using these patterns:
app.get() → fastify.get()req/res → request/replyres.json(data) → return datares.status(code).json(data) → reply.code(code); return datares.header() → reply.header()Add JSON schema for validation (REQUIRED for every route):
fastify.get('/example', {
schema: {
querystring: {
type: 'object',
properties: {
query: { type: 'string' }
}
},
response: {
200: {
type: 'object',
properties: {
data: { type: 'array' }
}
}
}
}
}, async (request, reply) => {
// handler
});
fastify.addHook('onRequest', ...)preHandler optionfastify.setErrorHandler(...)Consult references/migration_patterns.md for detailed examples.
STOP: Confirm all routes migrated with schemas before Phase 4.
Update the server entry point:
const app = Fastify({ logger: true });
app.register(helmet);
app.register(rateLimit, { max: 100, timeWindow: '1 minute' });
app.setErrorHandler((error, request, reply) => {
request.log.error(error);
reply.status(error.statusCode || 500).send({ error: error.message });
});
await app.listen({ port: PORT, host: '0.0.0.0' });
STOP: Confirm server setup complete before Phase 5.
You MUST complete ALL of these checks:
npm run build - MUST succeed with no errorsnpm test - ALL tests MUST pass@fastify/helmet, @fastify/rate-limit)Do NOT report completion until ALL boxes are checked.
Compare Fastify responses against the Express responses saved in Phase 1.
For EACH endpoint, verify the output matches:
# Capture Fastify response
curl -s "http://localhost:PORT/endpoint" | jq . > /tmp/fastify_endpoint.json
# Compare against Express baseline from Phase 1
diff /tmp/express_endpoint.json /tmp/fastify_endpoint.json
You MUST verify:
If outputs differ, fix the Fastify implementation before proceeding.
npx autocannon -c 10 -d 10 http://localhost:PORT/endpoint
| Metric | Express (Phase 1) | Fastify (Phase 6) | Improvement |
|---|---|---|---|
| Req/sec | baseline | new | X% |
| Latency | baseline | new | X% |
| Throughput | baseline | new | X% |
Expected improvements:
STOP: You MUST report BOTH the output compatibility verification AND the performance comparison table before Phase 7.
After performance verification is complete:
npm uninstall express morgan compression cors cookie-parser express-session
npm uninstall @types/express @types/morgan @types/compression @types/cors
Remove any Express-specific code that was kept for benchmarking
Final verification:
npm run build - MUST succeednpm test - ALL tests MUST passDo NOT report migration complete until Express is fully removed.
Consult these files for detailed patterns:
references/migration_patterns.md - Route and middleware conversion examplesreferences/plugin_ecosystem.md - Complete Express → Fastify plugin mappingscripts/benchmark.js - Performance benchmarkingscripts/schema_generator.js - Generate JSON schemas from examplesassets/server_template.js - Production-ready Fastify boilerplatereturn, not reply.send(JSON.stringify())afterAll(() => app.close())For typed routes:
interface QueryParams {
query?: string;
}
fastify.get<{ Querystring: QueryParams }>(
'/search',
{ schema: { querystring: { type: 'object', properties: { query: { type: 'string' } } } } },
async (request, reply) => {
const { query } = request.query; // Typed!
return { results: [] };
}
);