菜单

Express 集成

相关源文件

本文档详细介绍了 NestJS 如何与 Express 集成,Express 是 NestJS 应用程序使用的默认 HTTP 服务器框架。它涵盖了集成的架构、如何在 NestJS 应用程序中访问和自定义 Express 功能,以及 Express 适配器实现的具体细节。有关 Fastify 集成的信息,请参阅 Fastify 集成

介绍

Express 是一个极简且灵活的 Node.js Web 应用框架,为 Web 和移动应用程序提供了强大的功能集。NestJS 使用 Express 作为其默认的 HTTP 平台适配器,提供了一个兼容层,允许 NestJS 应用程序利用 Express 的生态系统,同时保持 NestJS 的结构化、意见驱动的架构。

来源:packages/platform-express/package.json1-36 packages/core/package.json1-64

架构

NestJS 中的 Express 集成遵循适配器模式,将平台特定代码与核心框架隔离开来。这种架构允许 NestJS 支持多个 HTTP 平台,同时保持一致的 API。

Express 适配器架构

来源:packages/core/adapters/http-adapter.ts1-176 packages/platform-express/adapters/express-adapter.ts1-482 packages/common/interfaces/http/http-server.interface.ts1-103

集成流程

来源:packages/core/nest-application.ts1-471 packages/platform-express/adapters/express-adapter.ts1-482

在 NestJS 中使用 Express

使用 Express 创建 NestJS 应用程序

默认情况下,当您使用 NestFactory.create() 创建新应用程序时,NestJS 会使用 Express。您可以显式地将 Express 指定为平台

来源:packages/platform-express/adapters/express-adapter.ts51-62

访问 Express 实例

您可以从 NestJS 应用程序访问底层的 Express 实例

来源:packages/platform-express/interfaces/nest-express-application.interface.ts20-29 packages/platform-express/adapters/express-adapter.ts188-190

Express 适配器功能

NestJS 中的 Express 适配器在保持 NestJS 结构化架构的同时,提供了对 Express 特定功能的访问。

中间件管理

来源:packages/platform-express/adapters/express-adapter.ts235-251 packages/platform-express/adapters/express-adapter.ts204-209 packages/platform-express/adapters/express-adapter.ts231-233

关键 Express 适配器方法

方法描述Express 等效
use(...)注册中间件express.use()
useStaticAssets(path, options)提供静态文件express.static()
setViewEngine(engine)设置模板引擎app.set('view engine', engine)
setBaseViewsDir(path)设置视图目录app.set('views', path)
enableCors(options)启用 CORScors() 中间件
set(name, value)设置应用程序设置app.set(name, value)
engine(ext, callback)注册模板引擎app.engine(ext, callback)
enable(setting)启用设置app.enable(setting)
disable(setting)禁用设置app.disable(setting)
useBodyParser(type, options)配置请求体解析器express.json(), express.urlencoded() 等。
setLocal(key, value)设置局部变量app.locals[key] = value

来源:packages/platform-express/adapters/express-adapter.ts188-302 packages/platform-express/interfaces/nest-express-application.interface.ts20-143

请求-响应处理

The ExpressAdapter 类实现了多个方法来处理 HTTP 请求和响应

响应方法

来源:packages/platform-express/adapters/express-adapter.ts62-129

请求方法

该适配器提供了从传入请求中提取信息的方法

方法描述
getRequestHostname(request)返回请求主机名
getRequestMethod(request)返回 HTTP 方法
getRequestUrl(request)返回请求 URL

来源:packages/platform-express/adapters/express-adapter.ts219-229

请求体解析

Express 适配器为处理 JSON 和 URL 编码的负载注册了请求体解析中间件

The useBodyParser 方法允许更精细地控制

来源:packages/platform-express/adapters/express-adapter.ts269-297

API 版本控制

Express 适配器通过 applyVersionFilter 方法实现了版本控制策略,支持

  • URI 版本控制
  • Header 版本控制
  • Media Type 版本控制
  • 自定义版本控制

这使得同一端点的不同版本可以共存在 NestJS 应用程序中。

来源:packages/platform-express/adapters/express-adapter.ts308-453

静态资源和视图

Express 适配器提供了用于提供静态文件和配置模板引擎的方法

来源:packages/platform-express/adapters/express-adapter.ts204-217 packages/platform-express/interfaces/serve-static-options.interface.ts1-80

性能考量

虽然 Express 因其成熟和广泛的生态系统而成为 NestJS 的默认 HTTP 平台,但值得注意的是,Fastify 在某些用例下可能提供更好的性能。在 Express 和 Fastify 之间进行选择时,请考虑您的应用程序需求。

结论

NestJS 中的 Express 集成提供了一种无缝的方式来利用 Express 丰富的生态系统,同时保持 NestJS 的结构化架构。The ExpressAdapter 作为 NestJS 的平台无关核心与 Express 特定功能之间的桥梁,使开发人员能够在 NestJS 框架中使用 Express 功能。

对于大多数 NestJS 应用程序,默认的 Express 集成提供了所需的所有 HTTP 服务器功能,并且可以在需要时访问 Express 特定的功能。对于具有特定性能要求的应用程序,也可以考虑评估 Fastify 集成。