Loading...
Loading...
Fastify high-performance Node.js framework. Covers routing, plugins, validation, and serialization. Use when building fast Node.js APIs. USE WHEN: user mentions "Fastify", "fastify", "TypeBox", "schema validation", asks about "fast Node.js framework", "high-throughput API", "JSON schema validation", "performance-critical backend", "plugin-based architecture" DO NOT USE FOR: Enterprise DI patterns - use `nestjs` instead, Minimalist approach - use `express` instead, Edge runtimes - use `hono` instead, Deno - use `oak` or `fresh` instead
npx skill4agent add claude-dev-suite/claude-dev-suite fastifyFull Reference: See advanced.md for WebSocket authentication, room management, heartbeat patterns, message validation, and Redis scaling.
Deep Knowledge: Usewith technology:mcp__documentation__fetch_docsfor comprehensive documentation.fastify
import Fastify from 'fastify';
import cors from '@fastify/cors';
const app = Fastify({ logger: true });
await app.register(cors);
await app.register(userRoutes, { prefix: '/api/users' });
app.listen({ port: 3000, host: '0.0.0.0' });import { FastifyPluginAsync } from 'fastify';
import { Type, Static } from '@sinclair/typebox';
const UserSchema = Type.Object({
name: Type.String(),
email: Type.String({ format: 'email' }),
});
type User = Static<typeof UserSchema>;
const routes: FastifyPluginAsync = async (app) => {
app.post<{ Body: User }>('/', {
schema: {
body: UserSchema,
response: { 201: UserSchema }
}
}, async (request, reply) => {
const user = await db.users.create(request.body);
reply.status(201).send(user);
});
};import fp from 'fastify-plugin';
// Custom plugin with fastify-plugin
export default fp(async (app) => {
app.decorate('db', db);
app.addHook('onRequest', async (request) => {
request.startTime = Date.now();
});
});
app.register(myPlugin);import { FastifyError, FastifyReply, FastifyRequest } from 'fastify';
export class AppError extends Error {
constructor(public statusCode: number, message: string) {
super(message);
}
}
app.setErrorHandler((error: FastifyError, request, reply) => {
request.log.error({ err: error }, 'Request error');
if (error.validation) {
return reply.status(400).send({ error: 'Validation failed', details: error.validation });
}
return reply.status(error.statusCode || 500).send({ error: error.message });
});app.get('/health', async () => ({ status: 'healthy' }));
app.get('/ready', async (request, reply) => {
try {
await app.pg.query('SELECT 1');
return { status: 'ready', database: 'connected' };
} catch (error) {
reply.status(503);
return { status: 'not ready' };
}
});import { test } from 'vitest';
import { buildApp } from '../src/app';
test('GET /api/users returns users', async () => {
const app = await buildApp();
const response = await app.inject({
method: 'GET',
url: '/api/users',
});
expect(response.statusCode).toBe(200);
await app.close();
});import websocket from '@fastify/websocket';
await app.register(websocket);
app.get('/ws', { websocket: true }, (socket, req) => {
socket.on('message', (data) => {
const message = JSON.parse(data.toString());
handleMessage(socket, message);
});
});| Anti-Pattern | Why It's Bad | Correct Approach |
|---|---|---|
| Not using schema validation | Loses performance advantage | Define schemas for routes |
Missing | Encapsulation issues | Use |
Using | Slow, port conflicts | Use |
| Ignoring response schema | Slower serialization | Define response schemas |
| Issue | Likely Cause | Solution |
|---|---|---|
| "Cannot call reply.send twice" | Multiple sends | Return after first |
| Plugin not accessible | Missing fastify-plugin | Wrap with |
| Schema not working | Not registered | Add |
| Type errors with schemas | Provider missing | Use |
| Metric | Target |
|---|---|
| Response time (p99) | < 50ms |
| Error rate (5xx) | < 0.1% |
| Request throughput | > 10k/s |