What is Fastify?
Fastify is a web framework for Node.js focused on speed and low overhead. It covers the same ground as Express — routes, middleware, request handling — but with a plugin system, built-in JSON-schema validation, and structured logging as first-class features.
A minimal server
import Fastify from 'fastify'
const app = Fastify({ logger: true })
app.get('/health', async () => ({ status: 'ok' }))
await app.listen({ port: 3000, host: '0.0.0.0' })
Handlers can simply return a value (or throw); there’s no res.send()
bookkeeping, and async functions work naturally.
Plugins
Everything in Fastify is a plugin: routes, auth, static files, database access. A feature is registered once and encapsulated, so it can’t accidentally leak state into the rest of the app:
await app.register(fastifyCookie)
await app.register(fastifyStatic, { root: publicDir })
await app.register(orderRoutes) // your own routes, as a plugin
Splitting an API into routes/auth.js, routes/orders.js, and so on —
each a plugin — keeps a small service tidy without needing a heavy
framework.
Things that come up in real services
- Raw request bodies: webhook signatures (for example Shopify’s HMAC) are computed over the exact bytes received. Parse JSON from a raw buffer with a custom content-type parser and keep the original, otherwise re-serialising the body changes it and the signature never matches.
trustProxy: behind a reverse proxy such as Caddy, enabletrustProxy: truesorequest.ipis the visitor’s address fromX-Forwarded-For. Without it, per-IP rate limiting sees every request as coming from the proxy.- Rate limiting:
@fastify/rate-limitcan be registered withglobal: falseand applied only to sensitive routes like login. - Static frontend:
@fastify/staticserves a no-build vanilla-JS UI from the same process as the API.
When to reach for Fastify
- A small-to-medium API or service where Express feels loose and a larger framework feels heavy
- You want validation, logging, and plugin encapsulation built in
- Throughput and latency matter
Further reading
Official docs: fastify.dev