-
20221221Wed_공부 일지> Backend/NestJS 2022. 12. 22. 00:50
1. Decorator
-> 다섯가지 종류: Method / Accessor / Property / Class / Param (pg.35~42)
2. Controller
-> 정의: 들어오는 request를 받고, 처리된 response를 돌려주는 인터페이스 역할.
-> 진행 방법: nest new project-name --skip-git => nest g resource name
<기본적인 controller 생김새>
@Get('/hello') // 경로중에 / 는 생략할 수 있다. getHello(): string { return this.appService.getHello(); }
<와일드 카드 사용한 controller 생김새>
//와일드카드: 아래와 같이 와일드 카드를 사용해서 라우팅 패스를 작성할 수 있다. @Get('/ho*') getHello2(): string { return this.appService.getHello(); }
<request 사용 => express에서 import 해줄것>
// 요청 객체: NestJs는 req와 함께 전달되는 데이터를 핸들러(요청을 처리할 구성 요소, 컨트롤러가 이 역할을 함)이 다룰수 있는 객체로 변환 // 요청 객체는 HTTP 요청을 의미한다. // 요청 객체(req)가 어떻게 구성요소로는 쿼리 스트링, 매개변수, 헤더, 바디 등 다양한 정보를 가지고 있지만 대부분 decorators (@Query(), @Param, @Body 등)을 사용해 처리한다. @Get('/hello3') getHello3(@Req() req: Request): string { // console.log(req) return this.appService.getHello(); }
<Request처리 방법>
@Get(':id') findOne(@Param('id') id: string) { if(+id < 1){ //문자로 받은 id앞에 +붙여주면, 숫자로 변환 throw new BadRequestException('id값은 0보다 큰 값이어야 합니다.') } return this.usersService.findOne(+id); }
3. 해맨 부분
1. nest new project-name-in-lowercase --skip-git => nest new automatically include .git folder which screw things up if you scaffold inside of existing git repo. => to avoid, use --skip-git 2. when the file is readonly => sudo chmod -R 777 fileDirectory => 파일이름 파일이 모두에게 권한 생김
'> Backend > NestJS' 카테고리의 다른 글
20221227화_공부 일지 (0) 2022.12.28 20221222Thu_공부 일지 (0) 2022.12.23 009_TypeORM을 NestJS에 연결 - 1 (0) 2022.05.27 007_NestJS_Service 와 Repository (0) 2022.05.18 006_NestJS_Pipes를 활용한 Validation Check (0) 2022.05.16