728x90
반응형
SMALL
노마드코더의 NestJS 강의를 듣고 작성하는 글입니다.
#0 INTRODUCTION
0.1 Welcome
OOP, FP, FRP 를 사용한다.
Express 와 Typescript 를 사용한다.
0.2 Requirements
Node.js, VSCode 설치, insomnia 설치 Rest client 이다.
이걸로 엔드포인트를 테스트할 수 있다.
0.3 Project Setup
CLI 설치
npm i -g @nestjs/cli
nest new [프로젝트명]
#1 ARCHITECTURE OF NESTJS
1.0 Overview
npm run start:dev
파이썬 FastAPI 처럼 데코레이터가 있다.
핫리로드가 적용된다.
1.1 Cotrollers
모듈은 user, photo 모듈과 같은 단위이다.
컨트롤러는 express 의 라우터이다. url 을 가져오고 함수를 실행한다.
데코레이터랑 함수 사이에 빈칸을 두면 안된다.
1.2 Services
구조와 아키텍처에 대해 얘기해보자.
컨트롤러: url 을 가져온다.
서비스: 비즈니스 로직
모든게 다 App module 에 들어갈 것이다.
#2 REST API
2.0 Movies Controller
create controller
nest g co
스펙은 테스트파일이다.
import { Controller, Get} from '@nestjs/common';
@Controller('movies')
export class MoviesController {
@Get()
getAll() {
return "This will return all movies"
}
}
@Controller('movies')
export class MoviesController {
@Get()
getAll() {
return "This will return all movies"
}
@Get("/:id")
getOne() {
return "This will return one movie"
}
}
@Controller('movies')
export class MoviesController {
@Get()
getAll() {
return "This will return all movies"
}
@Get("/:id")
getOne(@Param("id") id: string) {
return `This will return one movie with the id: ${id}`
}
}
@Get 과 @Param 은 같아야 하지만, 파라미터 변수는 달라도 된다.
export class MoviesController {
@Get()
getAll() {
return "This will return all movies"
}
@Get("/:id")
getOne(@Param("id") movieId: string) {
return `This will return one movie with the id: ${movieId}`
}
}
Patch 는 일부 리소스만 업데이트 한다.
import { Controller, Delete, Get, Param, Patch, Post} from '@nestjs/common';
@Controller('movies')
export class MoviesController {
@Get()
getAll() {
return "This will return all movies"
}
@Get("/:id")
getOne(@Param("id") movieId: string) {
return `This will return one movie with the id: ${movieId}`
}
@Post()
create() {
return `This will create a movie`
}
@Delete("/:id")
remove(@Param("id") movieId: string) {
return `This will remove a movie`
}
@Patch("/:id")
update(@Param("id") movieId: string) {
return `This will update a movie`
}
}
2.1 More Routes
728x90
반응형
LIST
'Backend > 노트' 카테고리의 다른 글
NestJS 로 배우는 백엔드 프로그래밍 (0) | 2024.03.31 |
---|---|
러닝 타입스크립트 (0) | 2024.03.30 |
파이썬으로 개발하는 빅데이터 기반 맛집 추천 서비스 (ft. Django, FastAPI) 초격차 패키지 Online (0) | 2023.12.22 |
FastAPI 를 사용한 파이썬 웹 개발 (2) | 2023.12.01 |
고랭 애플로그인 구현 (golang apple login) (0) | 2023.05.06 |