HTTP module
HTTP module
Axiosλ μμ κ°μ μ ν리μΌμ΄μ
μμ λ리 μ¬μ©λλ νλΆν κΈ°λ₯μ HTTP ν΄λΌμ΄μΈνΈμ
λλ€. κ·Έλ κΈ° λλ¬Έμ Nestλ μ΄ ν¨ν€μ§λ₯Ό κ°μΈμ κΈ°λ³Έμ μΌλ‘ λ΄μ₯λ HttpModuleλ‘ κ³΅κ°ν©λλ€. HttpModuleμ λ¨μν HTTP μμ²μ μννκΈ° μν΄ Axios κΈ°λ° λ©μλλ₯Ό λ
ΈμΆνλ HttpServiceλ₯Ό λ΄λ³΄λ΄κ³  λ¦¬ν΄ μ νμ Observablesλ‘ λ³νν©λλ€.
HttpServiceλ₯Ό μ¬μ©νκΈ° μν΄μλ HttpModuleμ κ°μ ΈμμΌν©λλ€.
@Module({
  imports: [HttpModule],
  providers: [CatsService],
})
export class CatsModule {}info ννΈ
HttpModuleμ@nestjs/commonν¨ν€μ§μμ 곡κ°λ©λλ€.
κ·Έλ° λ€μ HttpServiceλ₯Ό μ½μ
ν  μ μμ΅λλ€. μ΄ ν΄λμ€λ @nestjs/common ν¨ν€μ§μμ μ½κ² μ‘μΈμ€ν  μ μμ΅λλ€.
@@filename()
@Injectable()
export class CatsService {
  constructor(private readonly httpService: HttpService) {}
  findAll(): Observable<AxiosResponse<Cat[]>> {
    return this.httpService.get('http://localhost:3000/cats');
  }
}
@@switch
@Injectable()
@Dependencies(HttpService)
export class CatsService {
  constructor(httpService) {
    this.httpService = httpService;
  }
  findAll() {
    return this.httpService.get('http://localhost:3000/cats');
  }
}λͺ¨λ  λ©μλλ Observable μ€λΈμ νΈλ‘ λ©νλ AxiosResponseλ₯Ό 리ν΄ν©λλ€.
Configuration
Axiosλ HttpServiceλ₯Ό λμ± κ°λ ₯νκ² λ§λ€κΈ° μν΄ νμ©ν  μ μλ λ§μ μ΅μ
μ μ κ³΅ν©λλ€. μ¬κΈ°μ λν΄ μμΈν μμ보μμμ€. κΈ°λ³Έ λΌμ΄λΈλ¬λ¦¬ μΈμ€ν΄μ€λ₯Ό ꡬμ±νλ €λ©΄ HttpModuleμ register()λ©μλλ₯Ό μ¬μ©νμμμ€.
@Module({
  imports: [
    HttpModule.register({
      timeout: 5000,
      maxRedirects: 5,
    }),
  ],
  providers: [CatsService],
})
export class CatsModule {}μ΄λ¬ν λͺ¨λ μμ±μ axios μμ±μλ‘ μ λ¬λ©λλ€.
Async configuration
λͺ¨λ μ΅μ
μ 미리 μ λ¬νλ λμ  λΉλκΈ°μμΌλ‘ μ λ¬νλ €λ κ²½μ°κ° μ’
μ’
 μμ΅λλ€. μ΄ κ²½μ°, λΉλκΈ° λ°μ΄ν°λ₯Ό μ²λ¦¬νλ λ€μν λ°©λ²μ μ κ³΅νλ registerAsync() λ©μλλ₯Ό μ¬μ©νμμμ€.
κ°λ₯ν 첫 λ²μ§Έ λ°©λ²μ ν©ν 리 κΈ°λ₯μ μ¬μ©νλ κ²μ λλ€.
HttpModule.registerAsync({
  useFactory: () => ({
    timeout: 5000,
    maxRedirects: 5,
  }),
});λΆλͺ
ν, μ°λ¦¬ ν©ν λ¦¬λ λ€λ₯Έ λͺ¨λ  κ²μ²λΌ νλν©λλ€ ( asyncμΌ μλ μκ³  injectμ ν΅ν΄ μμ‘΄μ±μ μ£Όμ
ν  μλ μμ΅λλ€).
HttpModule.registerAsync({
  imports: [ConfigModule],
  useFactory: async (configService: ConfigService) => ({
    timeout: configService.getString('HTTP_TIMEOUT'),
    maxRedirects: configService.getString('HTTP_MAX_REDIRECTS'),
  }),
  inject: [ConfigService],
});λλ ν©ν 리 λμ ν΄λμ€λ₯Ό μ¬μ©ν μ μμ΅λλ€.
HttpModule.registerAsync({
  useClass: HttpConfigService,
});μμ ꡬμ±μ HttpModuleλ΄μμ HttpConfigServiceλ₯Ό μΈμ€ν΄μ€ν νκ³  μ΅μ
 κ°μ²΄λ₯Ό λ§λλ λ° νμ©ν©λλ€. HttpConfigServiceλ HttpModuleOptionsFactory μΈν°νμ΄μ€λ₯Ό ꡬνν΄μΌν©λλ€.
@Injectable()
class HttpConfigService implements HttpModuleOptionsFactory {
  createHttpOptions(): HttpModuleOptions {
    return {
      timeout: 5000,
      maxRedirects: 5,
    };
  }
}HttpModule λ΄λΆμ HttpConfigServiceκ° μμ±λλ κ²μ λ°©μ§νκ³  λ€λ₯Έ λͺ¨λμμ κ°μ Έμ¨ 곡κΈμλ₯Ό μ¬μ©νλ €λ©΄ useExisting ꡬ문μ μ¬μ©ν  μ μμ΅λλ€.
HttpModule.registerAsync({
  imports: [ConfigModule],
  useExisting: ConfigService,
});HttpModuleμ κ°μ Έμ¨ λͺ¨λμ κ²μνμ¬ μ체μ μΌλ‘ μΈμ€ν΄μ€ννλ λμ  μ΄λ―Έ μμ±λ ConfigServiceλ₯Ό μ¬μ¬μ©ν©λλ€.
Last updated
Was this helpful?