Performance

Performance

๊ธฐ๋ณธ์ ์œผ๋กœ Nest๋Š” Express ํ”„๋ ˆ์ž„ ์›Œํฌ๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ์•ž์—์„œ ์–ธ๊ธ‰ํ–ˆ๋“ฏ์ด Nest๋Š” ์˜ˆ๋ฅผ ๋“ค์–ด Fastify์™€ ๊ฐ™์€ ๋‹ค๋ฅธ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์™€์˜ ํ˜ธํ™˜์„ฑ๋„ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค. Nest๋Š” ๋ฏธ๋“ค์›จ์–ด ๋ฐ ํ•ธ๋“ค๋Ÿฌ๋ฅผ ์ ์ ˆํ•œ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ํŠน์ • ๊ตฌํ˜„์œผ๋กœ ํ”„๋ก์‹œํ•˜๋Š” ๊ธฐ๋ณธ ๊ธฐ๋Šฅ์„ ๊ฐ€์ง„ ํ”„๋ ˆ์ž„ ์›Œํฌ ์–ด๋Œ‘ํ„ฐ๋ฅผ ๊ตฌํ˜„ํ•˜์—ฌ์ด ํ”„๋ ˆ์ž„ ์›Œํฌ ๋…๋ฆฝ์„ฑ์„ ๋‹ฌ์„ฑํ•ฉ๋‹ˆ๋‹ค.

info ํžŒํŠธ ํ”„๋ ˆ์ž„ ์›Œํฌ ์–ด๋Œ‘ํ„ฐ๋ฅผ ๊ตฌํ˜„ํ•˜๋ ค๋ฉด ๋Œ€์ƒ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๊ฐ€ Express์—์žˆ๋Š” ๊ฒƒ๊ณผ ์œ ์‚ฌํ•œ ์š”์ฒญ/์‘๋‹ต ํŒŒ์ดํ”„ ๋ผ์ธ ์ฒ˜๋ฆฌ๋ฅผ ์ œ๊ณตํ•ด์•ผํ•ฉ๋‹ˆ๋‹ค.

Fastify๋Š” Express์™€ ์œ ์‚ฌํ•œ ๋ฐฉ์‹์œผ๋กœ ์„ค๊ณ„ ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•˜๊ธฐ ๋•Œ๋ฌธ์— Nest์— ์ ํ•ฉํ•œ ๋Œ€์ฒด ํ”„๋ ˆ์ž„ ์›Œํฌ๋ฅผ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋Ÿฌ๋‚˜ Fastify๋Š” Express๋ณด๋‹ค ๋น ๋ฅด๊ธฐ ๋•Œ๋ฌธ์— ๋ฒค์น˜ ๋งˆํฌ ๊ฒฐ๊ณผ๊ฐ€ ๊ฑฐ์˜ 2 ๋ฐฐ ํ–ฅ์ƒ๋ฉ๋‹ˆ๋‹ค. ์ ์ ˆํ•œ ์งˆ๋ฌธ์€ Nest๊ฐ€ Express๋ฅผ ๊ธฐ๋ณธ HTTP ๊ณต๊ธ‰์ž๋กœ ์‚ฌ์šฉํ•˜๋Š” ์ด์œ ๋Š” ๋ฌด์—‡์ž…๋‹ˆ๊นŒ? ๊ทธ ์ด์œ ๋Š” Express๊ฐ€ ๋„๋ฆฌ ์‚ฌ์šฉ๋˜๊ณ  ์žˆ์œผ๋ฉฐ, Nest ์‚ฌ์šฉ์ž๊ฐ€ ์ฆ‰์‹œ ์‚ฌ์šฉํ•  ์ˆ˜์žˆ๋Š” ๊ด‘๋ฒ”์œ„ํ•œ ํ˜ธํ™˜ ๊ฐ€๋Šฅํ•œ ๋ฏธ๋“ค์›จ์–ด ์„ธํŠธ๊ฐ€ ์žˆ๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค.

๊ทธ๋Ÿฌ๋‚˜ Nest๋Š” ํ”„๋ ˆ์ž„ ์›Œํฌ ๋…๋ฆฝ์„ฑ์„ ์ œ๊ณตํ•˜๋ฏ€๋กœ ์ด๋“ค๊ฐ„์— ์‰ฝ๊ฒŒ ๋งˆ์ด๊ทธ๋ ˆ์ด์…˜ ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋งค์šฐ ๋น ๋ฅธ ์„ฑ๋Šฅ์— ๋†’์€ ๊ฐ€์น˜๋ฅผ ๋ถ€์—ฌ ํ•  ๋•Œ Fastify๊ฐ€ ๋” ๋‚˜์€ ์„ ํƒ์ด ๋  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. Fastify๋ฅผ ํ™œ์šฉํ•˜๋ ค๋ฉด ์ด ์ฑ•ํ„ฐ์—์„œ์™€ ๊ฐ™์ด ๋‚ด์žฅ๋œ FastifyAdapter๋ฅผ ์„ ํƒํ•˜์‹ญ์‹œ์˜ค.

Installation

๋จผ์ € ํ•„์š”ํ•œ ํŒจํ‚ค์ง€๋ฅผ ์„ค์น˜ํ•ด์•ผํ•ฉ๋‹ˆ๋‹ค.

$ npm i --save @nestjs/platform-fastify

Adapter

Fastify ํ”Œ๋žซํผ์ด ์„ค์น˜๋˜๋ฉด FastifyAdapter๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

// src/main.ts
import { NestFactory } from '@nestjs/core';
import {
  FastifyAdapter,
  NestFastifyApplication,
} from '@nestjs/platform-fastify';
import { ApplicationModule } from './app.module';

async function bootstrap() {
  const app = await NestFactory.create<NestFastifyApplication>(
    ApplicationModule,
    new FastifyAdapter()
  );
  await app.listen(3000);
}
bootstrap();

๊ธฐ๋ณธ์ ์œผ๋กœ Fastify๋Š” localhost 127.0.0.1 ์ธํ„ฐํŽ˜์ด์Šค์—์„œ๋งŒ ์ˆ˜์‹ ํ•ฉ๋‹ˆ๋‹ค (๋” ์ฝ๊ธฐ). ๋‹ค๋ฅธ ํ˜ธ์ŠคํŠธ์—์„œ ์—ฐ๊ฒฐ์„ ์ˆ˜๋ฝํ•˜๋ ค๋ฉด listen() ํ˜ธ์ถœ์—์„œ '0.0.0.0'์„ ์ง€์ •ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.

async function bootstrap() {
  const app = await NestFactory.create<NestFastifyApplication>(
    ApplicationModule,
    new FastifyAdapter()
  );
  await app.listen(3000, '0.0.0.0');
}

Platform specific packages

FastifyAdapter๋ฅผ ์‚ฌ์šฉํ•  ๋•Œ Nest๋Š” HTTP ์ œ๊ณต์ž๋กœ Fastify๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ์ด๊ฒƒ์€ Express์— ์˜์กดํ•˜๋Š” ๊ฐ ๋ ˆ์‹œํ”ผ๊ฐ€ ๋” ์ด์ƒ ์ž‘๋™ํ•˜์ง€ ์•Š์„ ์ˆ˜ ์žˆ์Œ์„ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค. ๋Œ€์‹  ๋™๋“ฑํ•œ ํŒจํ‚ค์ง€๋ฅผ ์‚ฌ์šฉํ•˜์‹ญ์‹œ์˜ค.

Redirect response

Fastify๋Š” Express์™€ ์•ฝ๊ฐ„ ๋‹ค๋ฅธ ๋ฆฌ๋””๋ ‰์…˜ ์‘๋‹ต์„ ์ฒ˜๋ฆฌํ•ฉ๋‹ˆ๋‹ค. Fastify๋กœ ์˜ฌ๋ฐ”๋ฅธ ๋ฆฌ๋””๋ ‰์…˜์„ ์ˆ˜ํ–‰ํ•˜๋ ค๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ƒํƒœ ์ฝ”๋“œ์™€ URL์„ ๋ชจ๋‘ ๋ฐ˜ํ™˜ํ•˜์‹ญ์‹œ์˜ค.

@Get()
index (@Res() res) {
  // send 302 redirect to /login
  res.status(302).redirect('/login');
}

Fastify options

FastifyAdapter ์ƒ์„ฑ์ž๋ฅผ ํ†ตํ•ด Fastify ์ƒ์„ฑ์ž์— ์˜ต์…˜์„ ์ „๋‹ฌํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค.

new FastifyAdapter({ logger: true });

Example

์‹ค์ œ ์‚ฌ๋ก€๋Š” ์—ฌ๊ธฐ์—์„œ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

Last updated