HTTP adapter

HTTP adapter

๊ฒฝ์šฐ์— ๋”ฐ๋ผ Nest ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ์ปจํ…์ŠคํŠธ ๋‚ด์—์„œ ๋˜๋Š” ์™ธ๋ถ€์—์„œ ๊ธฐ๋ณธ HTTP ์„œ๋ฒ„์— ์•ก์„ธ์Šคํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

๊ธฐ๋ณธ์ ์œผ๋กœ ๋ชจ๋“  ๊ณ ์œ  (ํ”Œ๋žซํผ ๋ณ„) HTTP ์„œ๋ฒ„/๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ์ธ์Šคํ„ด์Šค๋Š” ์–ด๋Œ‘ํ„ฐ๋กœ ๋ž˜ํ•‘๋ฉ๋‹ˆ๋‹ค. ์–ด๋Œ‘ํ„ฐ๋Š” ์‘์šฉ ํ”„๋กœ๊ทธ๋žจ ์ปจํ…์ŠคํŠธ์—์„œ ๊ฐ€์ ธ ์™€์„œ ๋‹ค๋ฅธ ๊ณต๊ธ‰์ž์—๊ฒŒ ์‰ฝ๊ฒŒ ์ฃผ์ž…ํ•  ์ˆ˜ ์žˆ๋Š” ์ „์—ญ์ ์œผ๋กœ ์‚ฌ์šฉ ๊ฐ€๋Šฅํ•œ ๊ณต๊ธ‰์ž๋กœ ๋“ฑ๋ก๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค.

Outside strategy

์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ์ปจํ…์ŠคํŠธ ์™ธ๋ถ€์—์„œ HttpAdapter๋ฅผ ๊ฐ€์ ธ ์˜ค๊ธฐ ์œ„ํ•ด getHttpAdapter()๋ฉ”์†Œ๋“œ๋ฅผ ํ˜ธ์ถœํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

@@filename()
const app = await NestFactory.create(ApplicationModule);
const httpAdapter = app.getHttpAdapter();

In-context strategy

์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ์ปจํ…์ŠคํŠธ ๋‚ด์—์„œ HttpAdapterHost๋ฅผ ๊ฐ€์ ธ ์˜ค๋ ค๋ฉด ๊ธฐ์กด์˜ ๋‹ค๋ฅธ ์ œ๊ณต์ž์™€ ๋™์ผํ•œ ๋ฐฉ์‹์œผ๋กœ ( constructor๋ฅผ ํ†ตํ•ด) ์ฃผ์ž…ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

@@filename()
export class CatsService {
  constructor(private readonly adapterHost: HttpAdapterHost) {}
}
@@switch
@Dependencies(HttpAdapterHost)
export class CatsService {
  constructor(adapterHost) {
    this.adapterHost = adapterHost;
  }
}

info ํžŒํŠธ HttpAdapterHost๋Š” @nestjs/core ํŒจํ‚ค์ง€์—์„œ ๊ฐ€์ ธ์˜ต๋‹ˆ๋‹ค.

Adapter host

์ง€๊ธˆ๊นŒ์ง€ ์šฐ๋ฆฌ๋Š” HttpAdapterHost๋ฅผ ์–ป๋Š” ๋ฐฉ๋ฒ•์„ ๋ฐฐ์› ๋‹ค. ๊ทธ๋Ÿฌ๋‚˜ ์—ฌ์ „ํžˆ ์‹ค์ œ 'HttpAdapter'๋Š” ์•„๋‹™๋‹ˆ๋‹ค. HttpAdapter๋ฅผ ์–ป์œผ๋ ค๋ฉด ๊ฐ„๋‹จํžˆ httpAdapter ์†์„ฑ์— ์•ก์„ธ์Šคํ•˜์‹ญ์‹œ์˜ค.

const adapterHost = app.get(HttpAdapterHost);
const httpAdapter = adapterHost.httpAdapter;

httpAdapter๋Š” ํ”„๋ ˆ์ž„ ์›Œํฌ๊ฐ€ ์‚ฌ์šฉํ•˜๋Š” HTTP ์–ด๋Œ‘ํ„ฐ์˜ ์‹ค์ œ ์ธ์Šคํ„ด์Šค์ž…๋‹ˆ๋‹ค. ExpressAdapter ๋˜๋Š” FastifyAdapter๊ฐ€ ๋  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค (๋‘ ํด๋ž˜์Šค ๋ชจ๋‘ AbstractHttpAdapter๋ฅผ ํ™•์žฅํ•จ).

๋ชจ๋“  ์–ด๋Œ‘ํ„ฐ๋Š” HTTP ์„œ๋ฒ„์™€ ์ƒํ˜ธ ์ž‘์šฉํ•  ์ˆ˜ ์žˆ๋Š” ๋ช‡๊ฐ€์ง€ ์œ ์šฉํ•œ ๋ฐฉ๋ฒ•์„ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋Ÿผ์—๋„ ๋ถˆ๊ตฌํ•˜๊ณ  ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ์ฐธ์กฐ์— ์ง์ ‘ ์•ก์„ธ์Šคํ•˜๋ ค๋ฉด getInstance() ๋ฉ”์†Œ๋“œ๋ฅผ ํ˜ธ์ถœํ•˜์‹ญ์‹œ์˜ค.

const instance = httpAdapter.getInstance();

Last updated