本文档介绍了如何在 NestJS 应用程序中使用 Fastify 作为 HTTP 提供程序,详细说明了适配器实现、配置选项和特定功能。有关默认 Express 集成的信息,请参阅 Express 集成。
Fastify 是一个高性能、低开销的 Node.js Web 框架,是构建 NestJS 应用程序时 Express 的绝佳替代方案。它注重性能,并提供自动模式验证、序列化和插件架构。
NestJS 通过其平台无关的设计支持 Fastify,使用适配器来抽象底层 HTTP 实现。FastifyAdapter 作为 NestJS 和 Fastify 之间的桥梁。
来源: packages/core/adapters/http-adapter.ts packages/platform-fastify/adapters/fastify-adapter.ts packages/platform-express/adapters/express-adapter.ts packages/core/nest-application.ts
要将 Fastify 与 NestJS 一起使用,您需要安装必要的包
此包包括 FastifyAdapter 和集成 Fastify 与 NestJS 的所有必需依赖项。
来源: packages/platform-fastify/package.json1-44
创建 Fastify 的 NestJS 应用程序
FastifyAdapter 构造函数接受 Fastify 服务器选项来定制其行为
来源: packages/platform-fastify/adapters/fastify-adapter.ts121-252
FastifyAdapter 类扩展了 AbstractHttpAdapter,实现了 NestJS 所需的 HTTP 服务器接口,同时将实际的请求处理委托给 Fastify。
来源: packages/platform-fastify/adapters/fastify-adapter.ts121-795 packages/core/adapters/http-adapter.ts1-175
来源: packages/platform-fastify/adapters/fastify-adapter.ts63-136
当请求进入使用 Fastify 的 NestJS 应用程序时,它会经过几个层
来源: packages/platform-fastify/adapters/fastify-adapter.ts262-425 packages/core/nest-application.ts183-208
FastifyAdapter 内置支持解析 JSON 和 URL 编码的 payload
来源: packages/platform-fastify/adapters/fastify-adapter.ts561-612 packages/platform-fastify/interfaces/nest-fastify-application.interface.ts52-70
通过 @fastify/static 插件支持提供静态文件
来源: packages/platform-fastify/adapters/fastify-adapter.ts500-507 packages/platform-fastify/interfaces/nest-fastify-application.interface.ts71-77
通过 @fastify/view 插件支持模板渲染
来源: packages/platform-fastify/adapters/fastify-adapter.ts509-522 packages/platform-fastify/interfaces/nest-fastify-application.interface.ts86-92
可以使用 @fastify/cors 插件来启用 CORS
来源: packages/platform-fastify/adapters/fastify-adapter.ts554-559 packages/platform-fastify/interfaces/nest-fastify-application.interface.ts78-84
Fastify 提供了一个内置的 inject() 方法,用于在不进行实际网络调用的情况下测试 HTTP 请求。NestJS 通过 FastifyAdapter 公开了此功能
来源: packages/platform-fastify/adapters/fastify-adapter.ts476-482 packages/platform-fastify/interfaces/nest-fastify-application.interface.ts93-99
Fastify 为 listen() 方法提供了灵活的选项。FastifyAdapter 通过 NestFastifyApplication 接口支持这些选项
来源: packages/platform-fastify/adapters/fastify-adapter.ts261-292 packages/platform-fastify/interfaces/nest-fastify-application.interface.ts102-127
Fastify 拥有一个强大的插件系统,可以通过 register() 方法与 NestJS 一起使用
来源: packages/platform-fastify/adapters/fastify-adapter.ts468-474 packages/platform-fastify/interfaces/nest-fastify-application.interface.ts37-49
在 NestJS 应用程序中从 Express 迁移到 Fastify 时,请注意以下主要区别
来源: packages/platform-fastify/adapters/fastify-adapter.ts packages/platform-express/adapters/express-adapter.ts
Fastify 的请求和响应对象与 Express 不同
| 功能 | Express | Fastify |
|---|---|---|
| 请求体 | req.body | req.body |
| 请求参数 | req.params | req.params |
| 请求查询 | req.query | req.query |
| 响应状态 | res.status(code) | reply.code(code) |
| 发送响应 | res.send(data) | reply.send(data) |
| 头 | res.set(name, value) | reply.header(name, value) |
| 重定向 | res.redirect(url) | reply.redirect(url) |
来源: packages/platform-fastify/adapters/fastify-adapter.ts367-449 packages/platform-express/adapters/express-adapter.ts62-152
Fastify 旨在成为最快的 Node.js Web 框架之一
与 Express 相比,这些优化可以显著提高性能,尤其适用于 JSON 密集型 API。
来源: packages/platform-fastify/package.json1-44
FastifyAdapter 通过各种策略为您的 API 端点实现版本支持
该适配器通过 Fastify 的约束系统处理版本约束。
来源: packages/platform-fastify/adapters/fastify-adapter.ts149-220 packages/platform-fastify/adapters/fastify-adapter.ts354-364
Fastify 支持 HTTP/2,可以通过适配器进行配置
来源: packages/platform-fastify/adapters/fastify-adapter.ts70-97
Fastify 在 NestJS 中的集成提供了 Express 的高性能替代方案,同时保持了 NestJS 框架的所有功能和架构模式。通过使用 FastifyAdapter,开发人员可以利用 Fastify 的速度和功能,包括其强大的插件系统、Schema 验证和高效的请求处理。这使其特别适合需要最大性能和可扩展性的应用程序。