본문 바로가기

Nestjs3

[NestJS] 파일 업로드(File upload) Multer ✔ 필요한 종속성 설치 Nest는 Express용 multer 미들웨어 패키지를 기반으로 하는 내장 모듈을 제공한다. $ npm i -D @types/multer ✔ 단일 파일 업로드 단일 파일을 업로드하려면 FileInterceptor()를 @UseInterceptors에 연결하고 @UploadedFile() 데코레이터를 사용하여 요청에서 file를 추출하면 된다. @Post('upload') @UseInterceptors(FileInterceptor('file')) uploadFile(@UploadedFile() file: Express.Multer.File) { console.log(file); } FileInterceptor()는 2개의 인수를 가진다. fieldName : multipart/fo.. 2023. 7. 25.
[NestJS] Jest 단위 테스트 Mock ✔ 필요한 종속성 설치 $ npm i --save-dev @nestjs/testing ✔ Mock Repository Repository를 Mocking 하는 이유는 서비스 계층에서 비즈니스 로직을 검증해야 하는데 Repository를 의존하고 있다. 그래서 의존하는 것에 따라 테스트가 실패할 수도 있고 성공할 수도 있다. 그러므로 비즈니스 로직에 실패하거나 성공하는 게 아닌 다른 요소로 인해 실패할 수도 있기 때문에 의존하는 것들을 가짜로 Mock으로 만들어서 의존성을 배제하여 비즈니스 로직만 테스트하게 만든다. // users/users.service.ts import { Injectable } from '@nestjs/common'; import { Repository } from 'typeorm'.. 2023. 7. 22.
[NestJS] Passport JWT 토큰 인증 구현 & 에러 핸들링 JWT(JSON Web Token)에 대한 내용은 아래의 글을 참조 JWT(JSON Web Token) 이 글은 NestJS에서 JWT, Passport-jwt를 사용하여 토큰 인증을 구현하다가 겪은 내용을 정리한 글이다. ✔ 필요한 종속성 설치 NestJS에서 JWT 토큰과 Passport-Jwt를 사용하기 위해서 먼저 필요한 종속성을 설치해야 한다. $ npm install --save @nestjs/passport passport @nestjs/jwt passport-jwt $ npm install --save-dev @types/passport-jwt ✔ 동작 흐름 로그인에 발급받은 토큰 값을 HTTP 헤더에 "Authorization: Bearer 토큰 값" 추가하고 요청 AuthGuard JW.. 2023. 7. 6.