# Fastify — Fast & Low Overhead Web Framework for Node.js > Fastify is a fast and low overhead web framework for Node.js, focused on providing the best developer experience with least overhead and a powerful plugin architecture. Schema-driven with JSON Schema validation built in. ## Install Save in your project root: ## Quick Use ```bash npm i fastify ``` ```js import Fastify from "fastify"; const app = Fastify({ logger: true }); app.get("/", async () => ({ hello: "world" })); app.post("/users", { schema: { body: { type: "object", required: ["name"], properties: { name: { type: "string" } }, }, }, handler: async (req) => ({ created: req.body }), }); await app.listen({ port: 3000 }); ``` ## Intro Fastify is a fast and low overhead web framework for Node.js. Benchmarks consistently show it handling 30K+ req/s — among the fastest Node frameworks. Built around schema validation (JSON Schema) and a lightweight plugin system. - **Repo**: https://github.com/fastify/fastify - **Stars**: 36K+ - **Language**: JavaScript - **License**: MIT ## What Fastify Does - **HTTP server** — built on Node http with optimized routing (find-my-way) - **Schema validation** — JSON Schema for body/params/query/headers - **Serialization** — fast JSON output via fast-json-stringify - **Plugin system** — encapsulated context with `fastify-plugin` - **Hooks** — onRequest, preHandler, onSend, onResponse lifecycle - **Logging** — Pino by default (fastest JSON logger) ## Architecture Plugin tree: each plugin creates an encapsulated context. Decorators attach state to request/reply/instance. Schema compilation happens once at startup for zero-cost runtime validation. ## Self-Hosting ```bash # Production NODE_ENV=production node server.js # Docker FROM node:20-alpine WORKDIR /app COPY package*.json ./ RUN npm ci --only=production COPY . . CMD ["node", "server.js"] ``` ## Key Features - ~2x faster than Express in benchmarks - Native TypeScript types - JSON Schema validation + serialization - Request lifecycle hooks - Plugin encapsulation - HTTP/2 support - Async/await first-class - Graceful shutdown built-in ## Comparison | Framework | Req/s | Schema | TS | |---|---|---|---| | Fastify | ~45K | JSON Schema | Yes | | Express | ~15K | Manual | Optional | | Koa | ~25K | Manual | Optional | | Hapi | ~20K | Joi | Good | ## 常见问题 FAQ **Q: 能替换 Express 吗?** A: 可以。有 `@fastify/express` 兼容层支持大部分 Express 中间件。 **Q: 为什么性能这么高?** A: 优化的路由(radix tree)+ 编译期 schema 序列化(fast-json-stringify)+ Pino 日志。 **Q: NestJS 可以用 Fastify 吗?** A: 可以。NestJS 官方支持 Fastify adapter,替换默认 Express。 ## 来源与致谢 Sources - Docs: https://www.fastify.dev - GitHub: https://github.com/fastify/fastify - License: MIT --- Source: https://tokrepo.com/en/workflows/267271ae-355f-11f1-9bc6-00163e2b0d79 Author: AI Open Source