# Fastify

**Fastify** 是一个面向 Node.js 的快速、低开销 Web 框架，其设计目标是在平台内置 HTTP 服务器之上尽可能少地增加额外负担，同时提供路由、基于模式的验证、序列化、钩子和插件系统。<sup>[1](https://github.com/fastify/benchmarks)</sup><sup> • </sup><sup>[2](https://fastify.dev/benchmarks/)</sup> 它的定位针对的是 Express 和 Koa 等较早的 Node.js 框架，其自行发布的基准测试显示，在单个 Node.js 实例上，它在吞吐量和延迟方面均领先于这些框架。<sup>[2](https://fastify.dev/benchmarks/)</sup>

| 关键事实 | 详情 |
|---|---|
| 它是什么 | 一个面向 Node.js 的快速、低开销 Web 框架<sup>[1](https://github.com/fastify/benchmarks)</sup> |
| 吞吐量（官方基准） | Fastify 5.12.5 为 50,373 请求/秒，Express 5.2.1 为 27,474 请求/秒，据称前者为后者的 1.8 倍，单 Node.js 实例<sup>[2](https://fastify.dev/benchmarks/)</sup> |
| 延迟与吞吐量 | 每请求 19.33 毫秒、9.03 MB/s，而 Express 5.2.1 为 35.89 毫秒、4.90 MB/s<sup>[2](https://fastify.dev/benchmarks/)</sup> |
| 验证 | 基于模式的方法，使用 JSON Schema（Draft 7 示例）；模式被编译为函数，由 Ajv v8 处理请求验证<sup>[3](https://fastify.dev/docs/latest/Reference/Validation-and-Serialization/)</sup> |
| 插件系统 | 插件通过 `fastify.register()` 激活；建议插件作者使用 `fastify-plugin` 模块<sup>[4](https://fastify.dev/docs/latest/Reference/TypeScript/)</sup> |
| 封装 | 一项基本特性：除根上下文外，Fastify 中的一切都是插件，每个上下文可以持有装饰器、钩子、插件和路由<sup>[5](https://fastify.dev/docs/v5.7.x/Reference/Encapsulation/)</sup> |
| 独立测量 | 第三方测试将 Fastify 定位在约为 Express 的 1.4 至 1.7 倍，低于官方的合成数据<sup>[6](https://nifra.dev/blog/fastify-vs-express)</sup> |

## 性能

Fastify 的基准测试页面指出，该框架从底层开始设计，目标是在 Node.js 之上尽可能少地增加额外负担，并发布了一份单个 Node.js 实例上框架开销的示例性对比。<sup>[2](https://fastify.dev/benchmarks/)</sup> 在每秒请求数表格中，Fastify 5.12.5 达到 50,373 请求/秒，领先于 Hono 4.13.9 的 45,102、H3 2.0.1-rc.29 的 39,767、Koa 3.2.1 的 38,917、Restify 12.0.0 的 32,424、Hapi 21.4.10 的 31,978，以及 Express 5.2.1 的 27,474。<sup>[2](https://fastify.dev/benchmarks/)</sup> 同一页面报告 Fastify 的延迟为 19.33 毫秒，为该组最低，吞吐量为 9.03 MB/s，为该组最高。<sup>[2](https://fastify.dev/benchmarks/)</sup>

项目本身将这些数据限定为示例性数据，并指出实际结果取决于硬件、Node.js 版本和工作负载，并邀请开发者使用官方 fastify/benchmarks 套件针对自己的路由复现这些结果。<sup>[2](https://fastify.dev/benchmarks/)</sup> 基准测试仓库同样声明，这些对比并不意图代表真实场景，而是给出框架开销的指示。<sup>[1](https://github.com/fastify/benchmarks)</sup> 基于真实工作负载的独立测量报告了更小的差距：一份已发布的针对 [PostgreSQL](https://www.edgepedia.org/zh/postgresql) 数据库的测试运行发现，Fastify 约为 Express 的 1.4 至 1.7 倍。<sup>[6](https://nifra.dev/blog/fastify-vs-express)</sup>

## 验证与序列化

Fastify 使用基于模式的方法，并推荐使用 [JSON Schema](https://www.edgepedia.org/zh/json-schema) 来验证路由和序列化输出。模式被编译为函数，验证由可自定义的依赖 Ajv v8 处理，用于请求验证。<sup>[3](https://fastify.dev/docs/latest/Reference/Validation-and-Serialization/)</sup> Fastify 仅在其媒体类型为 `application/json` 时尝试验证载荷；如果主体模式定义了 `content` 属性来为各个媒体类型设置验证，则会出现例外。<sup>[3](https://fastify.dev/docs/latest/Reference/Validation-and-Serialization/)</sup> 当模式验证失败时，Fastify 会自动返回状态码 400 的响应，并在载荷中包含验证器的结果。<sup>[3](https://fastify.dev/docs/latest/Reference/Validation-and-Serialization/)</sup>

文档中包含一条安全警告：模式定义应被视为应用程序代码，因为验证和序列化功能使用了 `new Function()`，这在用户提供模式的情况下是不安全的。<sup>[3](https://fastify.dev/docs/latest/Reference/Validation-and-Serialization/)</sup> 该项目发布的安全公告包括一例通过不完整的模式大小写规范化绕过头部验证的漏洞。<sup>[7](https://github.com/fastify/fastify/security/advisories)</sup>

## 插件与封装

Fastify 通过插件扩展其功能，插件可以是一组路由、一个服务器装饰器或其他附加项，通过 `fastify.register()` 方法激活。<sup>[4](https://fastify.dev/docs/latest/Reference/TypeScript/)</sup> 插件生态系统在文档中被描述为 Fastify 最显著的特征之一。<sup>[4](https://fastify.dev/docs/latest/Reference/TypeScript/)</sup> 由 Fastify 团队维护的插件被列为核心插件，包括用于提供动态生成的 [Swagger](https://www.edgepedia.org/zh/swagger-software)/OpenAPI 文档的 `@fastify/swagger`、用于加载目录中所有插件的 `@fastify/autoload`、用于运行多个认证函数的 `@fastify/auth`，以及用于在 [AWS Lambda](https://www.edgepedia.org/zh/aws-lambda) 和 Amazon API Gateway 上构建无服务器应用的 `@fastify/aws-lambda`；社区插件单独列出。<sup>[8](https://fastify.dev/docs/latest/Guides/Ecosystem/)</sup>

封装是插件系统的结构性对应物。Fastify 的一项基本特性是封装上下文：除根上下文外，Fastify 中的一切都是插件，每个上下文可以由装饰器、钩子、插件和路由组成。<sup>[5](https://fastify.dev/docs/v5.7.x/Reference/Encapsulation/)</sup> 注册新插件会创建一个新的封装上下文。<sup>[9](https://fastify.dev/docs/latest/Reference/Hooks/)</sup> 钩子通过 `fastify.addHook` 方法注册，监听应用程序或请求/响应生命周期中的特定事件，并且由于它们受封装影响，因此可以应用于选定的路由。<sup>[9](https://fastify.dev/docs/latest/Reference/Hooks/)</sup>

## TypeScript 支持

Fastify 提供一流的 [TypeScript](https://www.edgepedia.org/zh/typescript) 类型定义。请求体和回复可以通过 JSON Schema 派生的泛型进行类型标注，因此访问模式中未声明的属性会在编译时引发类型错误，而声明的属性则能通过类型检查。<sup>[4](https://fastify.dev/docs/latest/Reference/TypeScript/)</sup> 插件类型得到完全支持，并利用了声明合并模式，即插件修改现有的 Fastify 类型接口以暴露其添加的装饰器。<sup>[4](https://fastify.dev/docs/latest/Reference/TypeScript/)</sup> 建议插件作者使用 `fastify-plugin` 模块；从 2.3.0 版本开始，它会自动为导出的插件添加 `.default` 属性和命名导出。<sup>[4](https://fastify.dev/docs/latest/Reference/TypeScript/)</sup> 在 `fastify.register()` 中使用的插件接口定义中，通用的 `FastifyPlugin` 被记录为已弃用，推荐使用 `FastifyPluginCallback` 和 `FastifyPluginAsync`，因为通用形式无法正确推断异步函数的类型。<sup>[4](https://fastify.dev/docs/latest/Reference/TypeScript/)</sup>

## 在 Node.js 框架中的定位

Fastify 常被与 Express、Koa、Hapi 和 NestJS 进行比较。一篇比较性综述将 Fastify 描述为强调极低的生产环境开销、强大的插件边界和基于模式的验证，同时指出 Express 的优势在于几乎每个 Node 开发者都能立即读懂它，且其生态系统极为庞大。<sup>[10](https://qungs.com/articles/nodejs-frameworks-express-fastify-nest-koa-compared)</sup> 针对 Node.js 框架的学术基准测试研究普遍发现，没有单一框架在每一项标准上都占主导地位；一项无服务器上下文研究对 Express、Fastify 和 NestJS 进行了性能基准测试，另一篇论文的结论是 Express 适合轻量级服务，而 Fastify 在性能与功能之间提供了良好的平衡。<sup>[11](https://www.diva-portal.org/smash/get/diva2%3A1968504/FULLTEXT01.pdf)</sup><sup> • </sup><sup>[12](https://kth.diva-portal.org/smash/get/diva2%3A1968504/FULLTEXT01.pdf)</sup> Fastify 也被用作研究系统中的 HTTP 层，包括一个采用 Svelte 和 Fastify 以及 API 网关的微前端电子商务案例研究。<sup>[13](https://arxiv.org/abs/2506.21297)</sup>

## References

1. [fastify/benchmarks repository README, GitHub](https://github.com/fastify/benchmarks)
2. [Benchmarks, Fastify official documentation](https://fastify.dev/benchmarks/)
3. [Validation and Serialization, Fastify latest documentation](https://fastify.dev/docs/latest/Reference/Validation-and-Serialization/)
4. [TypeScript, Fastify latest documentation](https://fastify.dev/docs/latest/Reference/TypeScript/)
5. [Encapsulation, Fastify v5.7.x documentation](https://fastify.dev/docs/v5.7.x/Reference/Encapsulation/)
6. [Fastify vs Express in 2026: measured, not vibes, Nifra](https://nifra.dev/blog/fastify-vs-express)
7. [Security Advisories, fastify/fastify, GitHub](https://github.com/fastify/fastify/security/advisories)
8. [Ecosystem, Fastify latest documentation](https://fastify.dev/docs/latest/Guides/Ecosystem/)
9. [Hooks, Fastify latest documentation](https://fastify.dev/docs/latest/Reference/Hooks/)
10. [Node.js Frameworks: How Express, Fastify, Nest, and Koa Compare, Qungs](https://qungs.com/articles/nodejs-frameworks-express-fastify-nest-koa-compared)
11. [Benchmarking Node.js frameworks in a serverless context, DiVA portal](https://www.diva-portal.org/smash/get/diva2%3A1968504/FULLTEXT01.pdf)
12. [Comparison of Node.js frameworks, KTH Royal Institute thesis, DiVA portal](https://kth.diva-portal.org/smash/get/diva2%3A1968504/FULLTEXT01.pdf)
13. [Exploring Micro Frontends: A Case Study Application in E-Commerce, arXiv](https://arxiv.org/abs/2506.21297)

---
*Initially written Sep 17, 2026 · Reviewed: Sep 19, 2026; Sep 30, 2026; Oct 15, 2026 · Edited: Sep 19, 2026; Sep 30, 2026; Oct 11, 2026; Oct 15, 2026; Oct 25, 2026 · Last review: Oct 15, 2026*

Topic: 技术与人造世界 › 计算技术与数字系统 › 软件与编程 › Development tools and collaboration infrastructure › Frameworks, libraries, and APIs › Web frameworks and libraries

Copyright 2026 Edgepedia

License: Edgepedia Community License 1.0, https://www.edgepedia.org/edgepedia/license

Source: https://www.edgepedia.org/zh/fastify

English original: https://www.edgepedia.org/fastify
