Fastify From Zero
Fastify is the Node framework you reach for when you want Express's simplicity but more speed, more structure, and validation built in rather than bolted on. Two ideas set it apart: it's genuinely fast (one of the quickest Node frameworks, partly because it compiles your JSON schemas into optimized validation and serialization), and it's schema-first — you describe each route's input and output with JSON Schema, and Fastify validates requests, serializes responses, and can generate docs from that one description. It's a popular, production-proven choice, especially for new TypeScript services.
The mental model has two pillars. First, a route is a handler plus a schema: you give Fastify the
method, path, an async (request, reply) handler, and a schema describing body/params/querystring/response
— and validation + fast serialization come for free. Second, everything is a plugin: routes, decorators,
and shared logic are registered as plugins, and plugins are encapsulated — what you register inside a
plugin is scoped to that plugin and its children, which is how Fastify keeps large apps organized. Hold
"routes carry schemas, and the app is a tree of encapsulated plugins," and Fastify makes sense.
📝 This teaches the framework — it assumes you know JavaScript/Node (functions,
async/await, modules — JavaScript From Zero); it's especially nice with TypeScript. It's most illuminating read against Express (the closest comparison) and over the node:http roots guide. Fastify runs on Node, so examples are shown with the commands to run them.
How to read this
Read in order — it grows one service (a small books API) using schemas and plugins from a single route to a tested, deployable REST API. Phases carry difficulty badges.
The phases
Part 1 — The core (🟢 → 🟡)
- What Fastify Is & Your First Server 🟢 — the instance, an async handler, and a running server.
- Routing & Schemas 🟡 — methods, params, and JSON-schema validation + serialization.
- The Plugin System 🔴 —
register, encapsulation, anddecorate.
Part 2 — A real API (🟡 → 🔴)
4. Hooks & the Lifecycle 🟡 — the request/reply lifecycle and hooks (onRequest, preHandler, …).
5. Building a REST API 🟡 — full CRUD with schemas, plugins, and a service.
6. Error Handling 🔴 — setErrorHandler, validation errors, and consistent responses.
Part 3 — Ship it (🟡 → 🟢)
7. Testing & Production 🟡 — app.inject(), logging, and deployment.
8. Where to Go Next 🟢 — Fastify vs Express/NestJS, the plugin ecosystem, and what to build.
The throughline: a route is a handler plus a schema, and the app is a tree of encapsulated plugins. That schema-first, plugin-based design is Fastify's whole personality.