> For the complete documentation index, see [llms.txt](https://jakekwak.gitbook.io/nestjs/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://jakekwak.gitbook.io/nestjs/techniques/performance.md).

# Performance

## Performance

기본적으로 Nest는 [Express](https://expressjs.com/) 프레임 워크를 사용합니다. 앞에서 언급했듯이 Nest는 예를 들어 [Fastify](https://github.com/fastify/fastify)와 같은 다른 라이브러리와의 호환성도 제공합니다. Nest는 미들웨어 및 핸들러를 적절한 라이브러리 특정 구현으로 프록시하는 기본 기능을 가진 프레임 워크 어댑터를 구현하여이 프레임 워크 독립성을 달성합니다.

> info **힌트** 프레임 워크 어댑터를 구현하려면 대상 라이브러리가 Express에있는 것과 유사한 요청/응답 파이프 라인 처리를 제공해야합니다.

[Fastify](https://github.com/fastify/fastify)는 Express와 유사한 방식으로 설계 문제를 해결하기 때문에 Nest에 적합한 대체 프레임 워크를 제공합니다. 그러나 Fastify는 Express보다 **빠르기** 때문에 벤치 마크 결과가 거의 2 배 향상됩니다. 적절한 질문은 Nest가 Express를 기본 HTTP 공급자로 사용하는 이유는 무엇입니까? 그 이유는 Express가 널리 사용되고 있으며, Nest 사용자가 즉시 사용할 수있는 광범위한 호환 가능한 미들웨어 세트가 있기 때문입니다.

그러나 Nest는 프레임 워크 독립성을 제공하므로 이들간에 쉽게 마이그레이션 할 수 있습니다. 매우 빠른 성능에 높은 가치를 부여 할 때 Fastify가 더 나은 선택이 될 수 있습니다. Fastify를 활용하려면 이 챕터에서와 같이 내장된 `FastifyAdapter`를 선택하십시오.

## Installation

먼저 필요한 패키지를 설치해야합니다.

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

## Adapter

Fastify 플랫폼이 설치되면 `FastifyAdapter`를 사용할 수 있습니다.

```typescript
// 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` 인터페이스에서만 수신합니다 ([더 읽기](https://www.fastify.io/docs/latest/Getting-Started/#your-first-server)). 다른 호스트에서 연결을 수락하려면 `listen()` 호출에서 `'0.0.0.0'`을 지정해야 합니다.

```typescript
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을 모두 반환하십시오.

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

## Fastify options

`FastifyAdapter` 생성자를 통해 Fastify 생성자에 옵션을 전달할 수 있습니다. 예를 들면 다음과 같습니다.

```typescript
new FastifyAdapter({ logger: true });
```

## Example

실제 사례는 [여기](https://github.com/nestjs/nest/tree/master/sample/10-fastify)에서 확인할 수 있습니다.
