企业🤖AI Agent构建引擎,智能编排和调试,一键部署,支持私有化部署方案 广告
## HTTP 模块 [Axios](https://github.com/axios/axios) 是功能很丰富的 `HTTP` 客户端, 广泛应用于许多应用程序中。这就是为什么 `Nest` 包装这个包, 并以内置模块 `HttpModule` 的形式暴露它。`HttpModule` 导出 `HttpService`, 它只是暴露了基于 Axios 的方法来执行 HTTP 请求, 而且还将返回类型转换为 `Observables`。 >你也可以直接使用包括 [got](https://github.com/sindresorhus/got) 或 [undici](https://github.com/nodejs/undici) 在内的任何通用的 Node.js 的 HTTP 客户端。 ### 安装 我们先安装需要的依赖来开始使用它 ```shell $ npm i --save @nestjs/axios ``` ### 开始使用 安装完成之后,想要使用 `HttpService` ,我们需要导入 `HttpModule` 。 ```typescript @Module({ imports: [HttpModule], providers: [CatsService], }) export class CatsModule {} ``` 接下来,使用构造函数来注入 `HttpService`。 > `HttpModule` 和 `HttpService` 是 `@nestjs/axios` 包提供的 ```typescript @Injectable() export class CatsService { constructor(private readonly httpService: HttpService) {} findAll(): Observable<AxiosResponse<Cat[]>> { return this.httpService.get('http://localhost:3000/cats'); } } ``` > `AxiosResponse` 是 `axios` 包( `$ npm i axios` )暴露的接口。 所有 `HttpService` 的方法都返回一个包裹在 `Observable` 对象内的 `AxiosResponse` 。 ### 配置 [Axios](https://github.com/axios/axios) 提供了许多选项,您可以利用这些选项来增加您的 `HttpService` 功能。[在这里](https://github.com/axios/axios#request-config)阅读更多相关信息。要配置底层的 Axios 实例,请使用 `HttpModule` 的 `register()` 方法。所有这些属性都将直接传递给底层的 Axios 构造函数。 ```typescript @Module({ imports: [ HttpModule.register({ timeout: 5000, maxRedirects: 5, }), ], providers: [CatsService], }) export class CatsModule {} ``` ### 异步配置 如果你要给模块异步地传递选项,就使用 `registerAsync()` 方法。就像大多数动态模块一样, Nest 提供了几种处理异步数据的方法。 一种方法是使用工厂函数: ```typescript HttpModule.registerAsync({ useFactory: () => ({ timeout: 5000, maxRedirects: 5, }), }); ``` 就像其他工厂提供者一样,我们的工厂函数可以是[异步](https://docs.nestjs.com/fundamentals/custom-providers#factory-providers-usefactory)的而且可以通过 `inject` 参数注入依赖。 ```typescript HttpModule.registerAsync({ imports: [ConfigModule], useFactory: async (configService: ConfigService) => ({ timeout: configService.getString('HTTP_TIMEOUT'), maxRedirects: configService.getString('HTTP_MAX_REDIRECTS'), }), inject: [ConfigService], }); ``` 或者,你可以使用类而不是工厂来配置 `HttpModule` ,如下面所示。 ```typescript HttpModule.registerAsync({ useClass: HttpConfigService, }); ``` 上面的构造将在 `HttpModule` 中实例化 `HttpConfigService`,并利用它来创建一个选项对象。注意这个例子, `HttpConfigService` 必须和下面所示的一样实现 `HttpModuleOptionsFactory` 接口。 `HttpModule` 会调用被提供的类的实例上的 `createHttpOptions()` 方法。 ```typescript @Injectable() class HttpConfigService implements HttpModuleOptionsFactory { createHttpOptions(): HttpModuleOptions { return { timeout: 5000, maxRedirects: 5, }; } } ``` 如果你想要重复使用一个已经存在的选项提供者而不是在 `HttpModule` 内创建一个私有的拷贝,使用 `useExisting` 语法。 ```typescript HttpModule.registerAsync({ imports: [ConfigModule], useExisting: ConfigService, }); ```