๐ NestJS HTTP Module – ์ธ๋ถ API ์์ฒญ ์์ ์ ๋ณต ๊ฐ์ด๋
๐ NestJS HTTP Module – ์ธ๋ถ API ์์ฒญ ์์ ์ ๋ณต ๊ฐ์ด๋
NestJS์์ ์ธ๋ถ API ์๋ฒ(REST, JSON-RPC ๋ฑ)์ ํต์ ํ๋ ค๋ฉด
@nestjs/axios ๊ธฐ๋ฐ์ HTTP ๋ชจ๋(HttpModule) ์ ์ฌ์ฉํ๋ ๊ฒ์ด ๊ฐ์ฅ ๊ฐํธํ๊ณ ๊ฐ๋ ฅํฉ๋๋ค.
Axios ๊ธฐ๋ฐ์ผ๋ก ์๋ํ๋ฏ๋ก ์ต์ํ ๋ฐฉ์์ผ๋ก API ์์ฒญ์ ๋ง๋ค ์ ์๊ณ ,
RxJS ์คํธ๋ฆผ๊ณผ๋ ์์ฐ์ค๋ฝ๊ฒ ๊ฒฐํฉ๋ฉ๋๋ค.
์ด๋ฒ ๊ธ์ NestJS ๊ณต์ ๋ฌธ์ - HTTP Module์ ๋ฐํ์ผ๋ก
๊ธฐ๋ณธ ์ฌ์ฉ๋ฒ, ๋น๋๊ธฐ ์ค์ , ์ ์ญ ๋ฑ๋ก, RxJS ์๋ต ์ฒ๋ฆฌ๋ฒ๊น์ง ์ค๋ฌด ์์ฃผ๋ก ์๋ด๋๋ฆฝ๋๋ค.
โ 1. ์ค์น ๋ฐ ์ํฌํธ
npm install @nestjs/axios axios
import { HttpModule } from '@nestjs/axios';
@Module({
imports: [HttpModule],
providers: [MyService],
})
export class MyModule {}
โ๏ธ HttpService๋ @nestjs/axios๊ฐ ์ ๊ณตํ๋ ์์กด์ฑ์ ๋๋ค.
โ 2. ๊ฐ๋จํ GET ์์ฒญ ์์
import { Injectable } from '@nestjs/common';
import { HttpService } from '@nestjs/axios';
import { firstValueFrom } from 'rxjs';
@Injectable()
export class MyService {
constructor(private readonly httpService: HttpService) {}
async getUser() {
const response = await firstValueFrom(
this.httpService.get('https://jsonplaceholder.typicode.com/users/1'),
);
return response.data;
}
}
โ๏ธ firstValueFrom()์ ์ฌ์ฉํ์ฌ RxJS Observable์ Promise๋ก ๋ณํ
โ๏ธ response.data์ ์ค์ ์๋ต ๋ณธ๋ฌธ์ด ๋ค์ด์์ต๋๋ค.
๐บ ๊ด๊ณ
์ธ๋ถ API๋ฅผ ํธ์ถํ๋ ์๋น์ค ํจ์๋ ๋๋ถ๋ถ์ ๋ฐฑ์๋์์ ํต์ฌ์ด ๋๋ ๊ธฐ๋ฅ์ ๋๋ค.
์ด ์ง์ ์ ์ ๋์ผ์ค ๊ด๊ณ ๊ฐ ๊ฐ์ฅ ์ง์ค๋ ์๊ฒ ๋ ธ์ถ๋ ์ ์๋ ์์น์ ๋๋ค.
<ins class="adsbygoogle"
style="display:block"
data-ad-client="ca-pub-XXXXXX"
data-ad-slot="YYYYYY"
data-ad-format="auto"
data-full-width-responsive="true"></ins>
โ 3. POST ์์ฒญ ๋ฐ ํค๋ ํฌํจ
async createPost() {
const payload = { title: 'New Post', body: '๋ด์ฉ', userId: 1 };
const headers = { 'Authorization': 'Bearer xxx' };
const response = await firstValueFrom(
this.httpService.post('https://jsonplaceholder.typicode.com/posts', payload, { headers }),
);
return response.data;
}
โ๏ธ headers ์ต์ ์ผ๋ก ์ธ์ฆ ํ ํฐ ๋ฑ๋ ์์ ๋กญ๊ฒ ์ ๋ฌ ๊ฐ๋ฅ
โ 4. ์ ์ญ HTTP ์ค์ (timeout, baseURL ๋ฑ)
@Module({
imports: [
HttpModule.register({
timeout: 5000,
maxRedirects: 5,
baseURL: 'https://api.example.com',
}),
],
})
โ๏ธ ๋ชจ๋ ์์ฒญ์ ๊ณตํต ์ ์ฉ๋๋ ๊ธฐ๋ณธ ์ต์ ์ค์
โ 5. ๋น๋๊ธฐ ๋ฑ๋ก (useFactory)
HttpModule.registerAsync({
useFactory: async (configService: ConfigService) => ({
timeout: configService.get('HTTP_TIMEOUT'),
baseURL: configService.get('API_BASE_URL'),
}),
inject: [ConfigService],
}),
โ๏ธ .env ์ค์ ํ์ผ์ ํตํด ๋์ ์ผ๋ก ๊ตฌ์ฑํ ์ ์์ด ์ค๋ฌด์์ ๋งค์ฐ ์ ์ฉ
โ 6. RxJS ์ฐ๋ ์์ (์๋ต ๊ฐ๊ณต)
return this.httpService.get('https://api.example.com/data').pipe(
map((res) => res.data.items),
catchError((error) => {
throw new Error('API ์คํจ: ' + error.message);
}),
);
โ๏ธ RxJS๋ฅผ ํ์ฉํ๋ฉด ์คํธ๋ฆฌ๋ฐ, ๋ฆฌ์กํฐ๋ธ ์ฒ๋ฆฌ๊ฐ ์ฌ์์ง๋๋ค.
โ ์ค๋ฌด ์ ์ฉ ํ ์์ฝ
์์ ๋ฐฉ๋ฒ
API ์๋ต Promise ๋ณํ | firstValueFrom() ๋๋ lastValueFrom() ์ฌ์ฉ |
๊ณตํต ํค๋, ํ์์์ | register() ๋๋ registerAsync() ํ์ฉ |
์๋ต ์คํธ๋ฆผ ์ฒ๋ฆฌ | pipe(map(), catchError())๋ก RxJS ์คํธ๋ฆผ ๋ณํ |
์ธ์ฆ ์ฒ๋ฆฌ | Authorization, API-Key ๋ฑ ํค๋ ์ง์ ์ง์ |
์ ์ญ ๊ณตํต ์๋ฌ ํธ๋ค๋ง | interceptors ๋๋ ๊ธ๋ก๋ฒ ์์ธ ํํฐ ์ถ์ฒ |
NestJS HTTP ๋ชจ๋,NestJS HttpService ์ฌ์ฉ๋ฒ,NestJS ์ธ๋ถ API ํธ์ถ,NestJS axios ์ฐ๋,NestJS ๋น๋๊ธฐ API ์์ฒญ,NestJS HttpModule register,NestJS HttpModule ์ ์ญ ์ค์ ,NestJS firstValueFrom,NestJS REST API ์ฐ๋,NestJS ์ค๋ฌด ์ธ๋ถ ์์ฒญ ์ฒ๋ฆฌ