Comments (2)
폴더 구조를 이렇게 레이어 (계층) 단위로 나눌 수도 있습니다!
from nestjsbasic.
📂NestJS 폴더 구조
src
├── main.ts
├── app.module.ts
├── app.controller.ts
├── app.service.ts
├── modules
│ ├── module1
│ │ ├── module1.module.ts
│ │ ├── module1.controller.ts
│ │ ├── module1.service.ts
│ │ └── ...
│ ├── module2
│ │ ├── module2.module.ts
│ │ ├── module2.controller.ts
│ │ ├── module2.service.ts
│ │ └── ...
│ └── ...
└── ...
NestJS는 모듈화, 의존성 주입을 강조하여 애플리케이션의 확장성과 유지보수성을 향상시킨다.
의존성 주입 (Dependency Injection) : 의존 관계를 외부에서 결정. 런타임 시점의 의존 관계를 외부에서 주입.
⚙️Module
@Module()
데코레이터가 달린 클래스
import { Module } from '@nestjs/common';
import { AppController } from './app.controller';
import { AppService } from './app.service';
@Module({
imports: [],
controllers: [AppController],
providers: [AppService],
})
export class AppModule {}
- 기본적으로 provider를 캡슐화
- 응용 프로그램 하나 당 적어도 1개 이상의 모듈 보유
- root module : Nest 내부 데이터 구조의 시작점이 되는 모듈
- 밀접한 기능끼리 모듈을 구성
⚙️Controller
클라이언트의 request를 처리하고, response를 반환
import { Controller, Get } from '@nestjs/common';
import { AppService } from './app.service';
@Controller()
export class AppController {
constructor(private readonly appService: AppService) {}
@Get()
getHello(): string {
return this.appService.getHello();
}
}
- class와
@Controller
데코레이터로 기본 컨트롤러 정의 @Get
,@Post
,@Delete
와 같은 Handler 포함- 클라이언트 입장에서 사용할 API에 대해 Routing 수행
- 라우팅 : 들어온 HTTP 요청을 특정 컨트롤러로 보내는 행위
@Get
은 HTTP 요청에 대한 특정 엔드 포인트 처리 (API 엔드포인트 : 클라이언트가 API에 접근할 수 있는 URL)
⚙️Provider
의존성을 주입할 수 있는 Nest의 기본 개념
- 어떤 컴포턴트가 필요하며 의존성을 주입 당하는 객체
- services, repositories, factories 등 여러 Nest class는 provider가 될 수 있음
⚙️Pipe
@Injectable()
데코레이터로 선언.
- transformation : input 데이터를 필요한 형식으로 변환
- validation : input 데이터를 평가하여 유효한 경우 전달, 아닌 경우 예외 처리
- Handler-level :
@UsePipe
데코레이터로 사용. 핸들러 레벨에서 모든 파라미터에 적용 - Parameter-level : 특정 파라미터에만 적용
- Global-level : main.ts에 넣어서 사용. 클라이언트에서 들어오는 모든 요청에 적용
⚙️DTO
Data Transfer Object. 계층 간 데이터 전송을 위해 도메인 모델 대신 사용되는 객체
- 순수하게 데이터 저장 및 getter, setter만을 가져야 함 (비지니스 로직 X)
- DTO 사용을 통해 도메인 모델을 캡슐화 하여 보호 가능
⚙️Middleware
클라이언트로부터 들어온 요청을 각 컨트롤러의 핸들러가 처리하기 전 코드를 실행할 수 있는 기능
- 미들웨어 함수는 request, response 객체에 접근 가능
- 미들웨어 함수는 일반적으로 next라는 변수로 표시
- 기본적으로 Express 미들웨어와 동일
- 코드 실행, req res 객체 변경, req res 주기 종료 등 수행 가능
from nestjsbasic.
Related Issues (6)
- docker-compose 사용하기 HOT 6
- 커머스 ERD 설계하기 HOT 15
- npm에 설치한 mysql? HOT 1
- eslint, prettier
- mapped-types 모듈로 DTO 재사용성 높이기 HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from nestjsbasic.