Fastify
Overview
Fastify is a fast, low-overhead web framework for Node.js focused on serving the highest number of requests possible without sacrificing security validation or developer experience. It is built around a plugin architecture, hooks, and JSON-Schema-based validation and serialization. Read this when building performance-sensitive HTTP APIs that benefit from schema-driven request handling.
Official sources
- Docs: https://fastify.dev/docs/latest/
- Repo: https://github.com/fastify/fastify
- Getting started: https://fastify.dev/docs/latest/Guides/Getting-Started/
Install / setup
npm i fastify
(Source: https://fastify.dev/docs/latest/Guides/Getting-Started/)
Core concepts
- Instance & lifecycle —
fastify()creates a server; routes and plugins register against it beforelisten(). - Plugins — almost everything is a plugin registered with
fastify.register(); plugins encapsulate routes, decorators, and hooks. - Encapsulation — each plugin gets its own context that inherits from parents but is isolated from siblings; use the
fastify-pluginwrapper to deliberately expose a plugin's decorators/hooks to the parent scope (https://fastify.dev/docs/latest/Reference/Encapsulation/). - JSON Schema validation & serialization — attach a
schemato a route to validate input and fast-serialize output. - Hooks — lifecycle hooks (
onRequest,preHandler,onSend, etc.) run code at defined request phases. - Decorators — extend
fastify,request, andreplywith custom properties/methods.
Best practices
- Define route schemas for body/querystring/params and responses to get validation plus faster serialization (https://fastify.dev/docs/latest/Guides/Getting-Started/).
- Prefer
async/awaithandlers; returning a value sends the response, and thrown errors are routed to the error handler. - Encapsulate features as plugins and only break encapsulation with
fastify-pluginwhen sharing is intentional (https://fastify.dev/docs/latest/Reference/Plugins/). - Register plugins and await readiness before calling
listen()so the plugin tree is fully booted.
Common pitfalls
- Mixing
asynchandlers with manualreply.send()and also returning a value → causes "reply already sent"; in async handlers, return the payload OR callreply.send, not both (https://fastify.dev/docs/latest/Reference/Routes/). - Expecting a decorator/hook registered inside a child plugin to be visible to parents → it isn't, due to encapsulation; wrap with
fastify-pluginto expose it upward.
Examples
import Fastify from 'fastify';
const fastify = Fastify({ logger: true });
fastify.get('/', async () => ({ hello: 'world' }));
await fastify.listen({ port: 3000 });
Further reading
- https://fastify.dev/docs/latest/Reference/Validation-and-Serialization/ — schema deep dive
- https://fastify.dev/docs/latest/Reference/Hooks/ — hook reference
Related skills
- ../express — the minimal framework Fastify positions against on performance
- ../nestjs — can run on a Fastify platform adapter