## 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,
});
```
- 介紹
- 概述
- 第一步
- 控制器
- 提供者
- 模塊
- 中間件
- 異常過濾器
- 管道
- 守衛
- 攔截器
- 自定義裝飾器
- 基礎知識
- 自定義提供者
- 異步提供者
- 動態模塊
- 注入作用域
- 循環依賴
- 模塊參考
- 懶加載模塊
- 應用上下文
- 生命周期事件
- 跨平臺
- 測試
- 技術
- 數據庫
- Mongo
- 配置
- 驗證
- 緩存
- 序列化
- 版本控制
- 定時任務
- 隊列
- 日志
- Cookies
- 事件
- 壓縮
- 文件上傳
- 流式處理文件
- HTTP模塊
- Session(會話)
- MVC
- 性能(Fastify)
- 服務器端事件發送
- 安全
- 認證(Authentication)
- 授權(Authorization)
- 加密和散列
- Helmet
- CORS(跨域請求)
- CSRF保護
- 限速
- GraphQL
- 快速開始
- 解析器(resolvers)
- 變更(Mutations)
- 訂閱(Subscriptions)
- 標量(Scalars)
- 指令(directives)
- 接口(Interfaces)
- 聯合類型
- 枚舉(Enums)
- 字段中間件
- 映射類型
- 插件
- 復雜性
- 擴展
- CLI插件
- 生成SDL
- 其他功能
- 聯合服務
- 遷移指南
- Websocket
- 網關
- 異常過濾器
- 管道
- 守衛
- 攔截器
- 適配器
- 微服務
- 概述
- Redis
- MQTT
- NATS
- RabbitMQ
- Kafka
- gRPC
- 自定義傳輸器
- 異常過濾器
- 管道
- 守衛
- 攔截器
- 獨立應用
- Cli
- 概述
- 工作空間
- 庫
- 用法
- 腳本
- Openapi
- 介紹
- 類型和參數
- 操作
- 安全
- 映射類型
- 裝飾器
- CLI插件
- 其他特性
- 遷移指南
- 秘籍
- CRUD 生成器
- 熱重載
- MikroORM
- TypeORM
- Mongoose
- 序列化
- 路由模塊
- Swagger
- 健康檢查
- CQRS
- 文檔
- Prisma
- 靜態服務
- Nest Commander
- 問答
- Serverless
- HTTP 適配器
- 全局路由前綴
- 混合應用
- HTTPS 和多服務器
- 請求生命周期
- 常見錯誤
- 實例
- 遷移指南
- 發現
- 誰在使用Nest?