Comments (2)
NestJS 만들면서 공부해보기
from wednesday_salon.
동작 Flow
-
Controller
-
클라이언트의 요청을 대응할 책임이 있는 곳 - 클라이언트의 요청을 대응해주는 계층
-
클라이언트와의 접점을 이루는 계층
-
Routing을 명시
@Controller('todos') export class TodoController { cosntructor( private todoService: TodoService // service를 주입받아서 사용 ) {} // @Get, @Post 등을 통해서 HTTP 요청 메서드를 정의 // 정의하면서 경로를 설정 할 수 있음 (와일드카드도 가능 - 'ab*cd') @Get() // http://localhost:3000/todos getTodos() { return this.todoService.getTodos() } // @Get(':id') // http://localhost:3000/todos/1 getTodoById(@Params('id') params) { return this.todoService.getTodoById(params.id) } @Post() // http://localhost:3000/todos - Post @HttpCode(204) // 응답에 대한 HTTP Code를 변경할 수 있음 (기본 200 / post는 201) createTodo(todo: TODO_DTO) { return this.todoService.createTodo(todo) } }
-
-
-
Provider
-
복잡한 로직을 위임받아서 작업하는 영역들
-
Module에서는 Provider로 정의 해서 관리
-
Service
- 공통으로 사용하는 상수나 함수, 기능을 모아놓은 단위
- Nest - Controller의 요청을 받아서
repository
에 전달 또는 DB에 요청하는 브릿지 역할
class TODO_DTO { id: number, content: string constructor(todo) { Object.assign(this, ...todo) } } @Injectable() // 데코레이터 등록해주어야, 다른곳에서도 주입받아서 쓸 수 있음. export class TodoService { private todos: Todo[] = [] getTodos() { return this.todos } getTodoById(id) { return this.todos.find(todo => todo.id === id) } createTodo(todo: TODO_DTO) { const newTodo = new TODO_DTO(todo) this.todos.push(newTodo) return newTodo } findOne(id) { return this.todos.find(todo => todo.id === id) } } // 의존성 주입 // 하나의 객체가 다른 객체(서비스로 사용할 수 있는 객체)의 의존성을 제공하는 테크닉 // 생성과 사용을 분리해서, 사용하는쪽에서는 생성에 관심을 갖지 않고, 자유롭게 사용만 하는 방법
-
Pipe
-
변환과 유효성 검사를 하는 역할
// 기본적으로 6가지의 파이프가 제공됨 //ValidationPipe, ParseIntPipe, ParseBoolPipe, ParseArrayPipe, ParseUUIDPipe, DefaultValuePipe type Data = { startDate: Date, endDate: Date } // TodoController @Get(':id') async findOne(@Param('id', ParseIntPipe) id: number) { // param으로 들어온 id가 number인지 확인 - 틀리면 controller - pipe에서 에러를 throw }
-
Custom Pipe
@Injectable() export class TodoPipe implements PipeTransform { transform(value: Data, metadata: ArgumentMetadata) { if (!this.isValidDate(value)) { throw new BadRequestException( `시작 날짜가 종료일보다 뒤일 수는 없습니다.`, ); } return value; } private isValidDate({ startDate, endDate }: Data) { return new Date(startDate) <= new Date(endDate); } } ... // TodoController @Get() async update(@Body(TodoPipe) data: Data) { ... } // 또는 @Get() @UsePipes(TodoPipe) async update(@Body() data: Data) { ... }
-
-
-
-
Module
-
해당 파트, 또는 Root의 시작점으로써 관계 및 종속성을 관리하는 역할
@Global() // 해당 모듈을 전역에서 사용할 수 있게 지정함 @Module({ controllers: [TodoController], // 모듈에서 정의된 컨트롤러 리스트 providers: [TodoService], // 해당 모듈 내에서 공유할 수 있는 프로바이더 리스트 imports: [], // 이 모듈에 필요한 외부 모듈의 리스트 (import) exports: [], // 외부에 공개할 프로바이더 리스트 }) export class AppModule {}
-
-
Decorator
- 전달받은 함수, 메서드의 동작을 수정해서 리턴하는 함수
- Decorator Pattern 어떤 객체에 책임을 덧붙이는 패턴
from wednesday_salon.
Related Issues (20)
- [21.05.5] 모각코 수요 스터디(온라인) HOT 6
- [21.05.12] 모각코 수요 스터디(온라인) HOT 4
- [21.05.19] 모각코 수요 스터디(온라인)
- [21.05.26] 모각코 수요 스터디(온라인) HOT 7
- [21.06.02] 모각코 수요 스터디(온라인) HOT 6
- [21.06.09] 모각코 수요 스터디(온라인)
- [21.06.16] 모각코 수요 스터디(온라인) HOT 2
- [21.06.23] 모각코 수요 스터디(온라인) HOT 4
- [21.06.30] 모각코 수요 스터디(온라인) HOT 3
- [21.07.07] 모각코 수요 스터디(온라인) HOT 3
- [21.07.14] 모각코 수요 스터디(온라인)
- [21.07.21] 모각코 수요 스터디(온라인)
- [21.07.28] 모각코 수요 스터디(온라인)
- [21.08.04] 모각코 수요 스터디(온라인) HOT 1
- [21.08.11] 모각코 수요 스터디(온라인) HOT 2
- [21.08.18] 모각코 수요 스터디(온라인) HOT 2
- [21.08.25] 모각코 수요 스터디(온라인)
- [21.09.08] 모각코 수요 스터디(온라인)
- [21.09.15] 모각코 수요 스터디(온라인)
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 wednesday_salon.