Fastify is the fastest Node.js web framework, and its plugin architecture makes it far more than a simple HTTP server.
Core API — Speed Meets Simplicity
import Fastify from 'fastify'
const fastify = Fastify({ logger: true })
fastify.get('/api/users', async (request, reply) => {
const { limit = 10, offset = 0 } = request.query
return db.user.findMany({ take: limit, skip: offset })
})
fastify.post('/api/users', {
schema: {
body: {
type: 'object',
required: ['name', 'email'],
properties: {
name: { type: 'string' },
email: { type: 'string', format: 'email' }
}
}
}
}, async (request) => {
return db.user.create({ data: request.body })
})
await fastify.listen({ port: 3000 })
JSON Schema Validation — Free Performance
Fastify validates AND serializes using JSON Schema, making it 2-3x faster than Express:
const userSchema = {
type: 'object',
properties: {
id: { type: 'string' },
name: { type: 'string' },
email: { type: 'string' },
createdAt: { type: 'string', format: 'date-time' }
}
}
fastify.get('/api/users/:id', {
schema: {
params: { type: 'object', properties: { id: { type: 'string' } } },
response: { 200: userSchema }
}
}, async (request) => {
return db.user.findUnique({ where: { id: request.params.id } })
})
Plugin System — Encapsulated Modules
async function authPlugin(fastify) {
fastify.decorateRequest('user', null)
fastify.addHook('preHandler', async (request) => {
const token = request.headers.authorization?.replace('Bearer ', '')
if (token) request.user = await verifyJWT(token)
})
}
async function usersRoutes(fastify) {
fastify.register(authPlugin)
fastify.get('/', async (req) => {
if (!req.user) throw fastify.httpErrors.unauthorized()
return db.user.findMany()
})
}
fastify.register(usersRoutes, { prefix: '/api/users' })
Real-World Use Case
A team migrated from Express to Fastify for their API gateway handling 10K req/sec. JSON Schema validation replaced 200 lines of Joi validators. Response serialization alone cut p95 latency from 45ms to 18ms. Same logic, half the code, twice the speed.
Fastify proves that "fast" and "easy" aren't opposites.
Build Smarter Data Pipelines
Need to scrape websites, extract APIs, or automate data collection? Check out my ready-to-use scrapers on Apify — no coding required.
Custom scraping solution? Email me at spinov001@gmail.com — fast turnaround, fair prices.
Top comments (0)