Skip to content
Tutorials Center
My learning

Topics

Lesson 1 of 39 ยท Getting Started

What Is Fastify?

Meet Fastify, the low-overhead Node.js web framework: what it is, why it is fast, how it compares with Express, and what you will build in this course.

Fastify is a web framework for Node.js. You use it to build HTTP servers: JSON APIs, microservices, backends for web and mobile apps, and server-rendered pages. It was designed around two goals: very low overhead per request, and a good developer experience built on schemas, plugins and hooks.

If you know Node's built-in http module, think of Fastify as the layer that handles the boring but error-prone parts for you: routing, reading and parsing bodies, validating input, serializing output, logging and error handling. If you know Express, most ideas will feel familiar, but Fastify makes different choices that matter once your project grows.

An honest note about this course. Fastify is an npm package, and the in-browser runner on this site cannot install npm packages. So every Fastify example you can run here uses a stand-in: a small look-alike named Fastify written for this course (about 150 lines, shown at the top of each example). It mimics the real API and the real response formats for the features each lesson teaches, but it is not the real framework and it is not fast. The code below the stand-in line is what you would write with real Fastify; only the import differs. Code that only works with the real package (such as npm install commands) is shown read-only.

Why people pick Fastify

Fast Low overhead per request.
Schemas Validate input, speed up output.
Plugins Encapsulated building blocks.
Logging Structured logs built in.
1 / 2

What makes Fastify different

Four design decisions explain most of what you will learn.

1. Schema-first. You can describe the shape of requests and responses with JSON Schema. Fastify uses the schema to validate input (before your handler runs) and to build a specialized serializer for output. A response schema makes JSON output faster and also acts as a filter that prevents leaking fields you did not mean to send. You will study this in chapter 3.

2. Plugins and encapsulation. In Fastify, almost everything is a plugin: a function that receives an instance and adds routes, hooks or decorators to it. A plugin gets its own scope, so what you add inside it does not leak out unless you say so. This makes large apps easy to split into independent pieces. Chapter 4 covers it.

3. Async-first. Handlers can be async functions, and you return the response body. Errors thrown inside become HTTP error responses. There is no mandatory next() callback.

4. Performance as a feature. Fastify uses a fast radix-tree router, precompiled validators and serializers, and a small request/response layer. Chapter 6 shows how to keep it fast.

Fastify and Express at a glance

Topic Express Fastify
Style Middleware chain with next() Lifecycle with hooks; async handlers
Validation Add a library yourself JSON Schema built in
Serialization JSON.stringify Schema-compiled serializer (optional)
Structure Routers and middleware Plugins with encapsulation
Logging Add a library Pino built in
Return value Call res.send() Return a value, or call reply.send()

Neither is "better" for every project. Express has a huge ecosystem and a long history; Fastify gives you more structure and speed by default. If you have used Express before, the Express tutorial is a good companion, and the ideas transfer in both directions.

Your first look at Fastify code

Here is the whole idea in a handful of lines. The stand-in sits at the top; the last lines are plain Fastify.

Node.js A Fastify app in five lines

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

Four things happened that you did not code by hand: the object you returned became JSON, the content-type header was set to application/json; charset=utf-8, the status defaulted to 200, and Content-Length was computed. app.inject() sends a request through the app without you opening a browser; real Fastify uses an in-process fake request (no network), the stand-in uses a loopback request, but you use it the same way. You will rely on inject() throughout this course, including in the testing chapter.

Unknown routes and defaults

Fastify answers routes you did not define with a JSON 404 in a fixed format, so clients can rely on it.

Node.js The default 404 and plain text replies

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

Returning a string gives text/plain; charset=utf-8; returning an object gives JSON. The 404 body has three fields, message, error and statusCode, the same shape Fastify uses for every error.

What you will build in this course

flowchart LR
    A["Getting started<br/>servers, options, config"] --> B["Routes and handlers"]
    B --> C["Schemas and validation"]
    C --> D["Plugins and encapsulation"]
    D --> E["Hooks, serialization, security"]
    E --> F["Testing, deployment, projects"]

The course path: from a first server to tested, deployable apps.

You will learn to write routes, read and reply to requests, validate input with schemas, split an app into plugins, add hooks, authenticate users, secure and test your API, and deploy it. Later chapters finish with complete projects.

What you need

  • A recent Node.js LTS (Fastify 5 requires Node 20 or newer).
  • Comfort with modern JavaScript: async/await, destructuring, ES modules. If async code is new, read the Node.js tutorial first.
  • A terminal and an editor for the real project parts.

To follow along on your machine you would run this (read-only here, because the runner cannot install packages):

Terminal
mkdir hello-fastify && cd hello-fastify
npm init -y
npm install fastify

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What is Fastify?

    Fastify is a Node.js web framework focused on low overhead, schemas, plugins and hooks.
  2. 2. What is the main benefit of a response schema?

    A response schema lets Fastify compile a fast serializer, and only the listed properties are sent.
  3. 3. What does a handler return value become in Fastify?

    In an async handler the returned value is sent as the response; objects become JSON.
  4. 4. Why does this course use a stand-in Fastify in its runnable examples?

    The runner has Node's standard library but no npm packages, so a small look-alike shows the same API and behavior.

Summary

  • Fastify is a fast, schema-first Node.js web framework built from plugins, hooks and decorators.
  • Handlers are usually async functions that return the response body.
  • JSON replies, the default 404 format, headers and status codes are handled for you.
  • In this course, runnable examples use a labelled stand-in; the code below the stand-in line is real Fastify code.
  • app.inject() lets you test a request without a network or browser.

Next: Your First Fastify Server