Nest.js
Nest는 효율적이고 확장 가능한 Node.js 서버 측 애플리케이션을 구축하기 위한 프레임워크이다.
Nest는 기본적으로 Express.js를 기반으로 작동하지만 Fastify로 변경이 가능하다.
Nest Project 구축
NestCLI 스타터 프로젝트
>> npm i -g @nestjs/cli
>> nest new project-name
Git 기반의 TypeScript 스타터 프로젝트
>> git clone https://github.com/nestjs/typescript-starter.git project
>> cd project
>> npm install
>> npm run start
기본 구조
my-project/
├ src/
| ├ app.controller.spec.ts
| ├ app.controller.ts
| ├ app.module.ts
| ├ app.service.ts
| └ main.ts
|
app.controller.ts
app.contoller.spec.ts
app.module.ts
app.service.ts
main.ts
- 핵심 기능을 사용하는 애플리케이션의 항목 파일,
NestFactory Nest 애플리케이션 인스턴스를 생성
import { NestFactory } from '@nestjs/core';
import { AppModule } from './app.module';
async function bootstrap() {
const app = await NestFactory.create(AppModule);
await app.listen(process.env.PORT ?? 3000);
}
bootstrap();
main.ts는 전체 애플리케이션을 시작하는 부트스트랩(bootstrap) 스크립트이다.
main.ts는 NestFactory를 사용해 루트 모듈(AppModule)로 부터 애플리케이션 인스턴스를 받고, 서버를 실행시켜 외부 요청을 받을 준비를 한다.
Platform
Nest는 플랫폼에 구애 받지 않느 프레임워크를 목표로 한다.
Nest에서 지원되는 HTTP 플랫폼은 express와 festify 두 가지가 있다.
platform-express : Express는 노드를 위한 유명한 미니멀 리스트 웹 프레임워크이며 Nest에서 기본 설정으로 지원한다.
platform-fastify : fastify는 최고의 효율성과 속도를 제공하는 데 중점을 둔 고성능 및 낮은 오버헤드 프레임워크이다.
const app = await NestFactory.create<NestExpressApplication>(AppModule);
Nest CLI
nest g [ order | alias ] [ name ]
| order |
alias |
description |
| application |
application |
Generate a new application workspace |
| class |
cl |
Generate a new class |
| configuration |
config |
Generate a CLI configuration file |
| controller |
co |
Generate a controller declaration |
| decorator |
d |
Generate a custom decorator |
| filter |
f |
Generate a filter declaration |
| gateway |
ga |
Generate a gateway declaration |
| guard |
gu |
Generate a guard declaration |
| interceptor |
itc |
Generate an interceptor declaration |
| interface |
itf |
Generate an interface |
| library |
lib |
Generate a new library within a monorepo |
| middleware |
mi |
Generate a middleware declaration |
| module |
mo |
Generate a module declaration |
| pipe |
pi |
Generate a pipe declaration |
| provider |
pr |
Generate a provider declaration |
| resolver |
r |
Generate a GraphQL resolver declaration |
| resource |
res |
Generate a new CRUD resource |
| service |
s |
Generate a service declaration |
| sub-app |
app |
Generate a new application within a monorepo |