Fastify is a high-performance web framework for Node.js designed with developer experience and minimal overhead as core principles. Built from the ground up with performance in mind, it leverages a powerful plugin architecture, JSON Schema-based validation, and schema-driven serialization to deliver throughput rates 2-4Γ faster than Express. The framework's encapsulation model ensures that decorators, hooks, and plugins remain isolated within their registration context, preventing unintended side effects across application boundaries. A critical mental model: Fastify compiles JSON schemas at startup into highly optimized functions, meaning validation and serialization happen at near-native speed rather than runtime interpretation cost typical of other frameworks.
What This Cheat Sheet Covers
This topic spans 24 focused tables and 196 indexed concepts, 138 flashcards. Below is a complete table-by-table outline of this topic, spanning foundational concepts through advanced details.
A jump-to index of every table row in this cheat sheet.
An interactive map of every table and concept in this topic.
Table 1: Server Initialization and Configuration
The options you pass to the fastify() factory shape how the whole server behaves β from logging and body-size guards to proxy trust, routing quirks, and protocol choice. These are the knobs you set once at startup, and getting trustProxy, bodyLimit, and the timeout values right early saves a lot of grief in production behind load balancers.
| Option | Example | Description | |
|---|---|---|---|
fastify({ logger: true }) | β’ Enables Pino logger with default 'info' level β’ accepts boolean or config object with level, stream, and serializers | ||
fastify({ bodyLimit: 2097152 }) | β’ Sets maximum request body size in bytes β’ defaults to 1048576 (1 MiB) to prevent memory exhaustion | ||
fastify({ trustProxy: true }) | β’ Enables proxied headers like X-Forwarded-For β’ required for accurate client IP behind load balancers or reverse proxies | ||
fastify({ ignoreTrailingSlash: true }) | β’ Treats /users and /users/ as same routeβ’ simplifies routing logic by normalizing URL patterns | ||
fastify({ caseSensitive: false }) | β’ Makes route paths case-insensitive β’ treats /Users and /users as identical routes | ||
fastify({ requestIdHeader: 'x-request-id' }) | β’ Reads existing request ID from header β’ useful for distributed tracing across microservices | ||
genReqId: (req) => uuid() | β’ Custom request ID generator function β’ defaults to auto-incrementing number for each instance | ||
fastify({ connectionTimeout: 30000 }) | β’ Sets idle connection timeout in milliseconds β’ defaults to 0 (no timeout) for HTTP/1.1 | ||
fastify({ keepAliveTimeout: 72000 }) | β’ Sets keep-alive timeout for reusing connections β’ defaults to 72 seconds to match Node.js behavior | ||
fastify({ maxParamLength: 200 }) | β’ Limits URL parameter length β’ defaults to 100 characters to prevent abuse | ||
fastify({ http2: true }) | β’ Enables HTTP/2 server with multiplexing and header compression β’ requires Node.js 8.8+ | ||
https: { key, cert } | β’ Provides TLS credentials for HTTPS server β’ accepts key and cert file contents or paths |