Server/Nest.js
-
[Nest.js] 로컬에서 https 서버 구동하고 postman으로 테스트 하기Server/Nest.js 2023. 4. 4. 17:27
[Nest.js] 로컬에서 https 서버 구동하고 postman으로 테스트 하기 진행하고 있는 프로젝트에서 refresh token을 secure, httpOnly 옵션으로 쿠키에 담아 전송해주기로 했다. 그런데 프로덕트 api 서버가 https 서버로 세팅되어 있기 때문에 로컬에서 쿠키가 전송되는지 테스트를 하기 위해서는 로컬 서버도 https로 구동해야 했다. 그래서 mkcert라는 라이브러리를 사용하여 postman으로 쿠키를 확인할 수 있도록 세팅했다. 1. mkcert로 인증서 만들기 1-1. 설치 npm install -g mkcert 1-2. CA 만들기 mkcert -install mkcert -CAROOT #CA 저장 위치 확인 1-3. 인증서 만들기 mkcert -cert-file [..
-
효율적인 API 문서 관리를 위한 Swagger 도입Server/Nest.js 2023. 3. 10. 16:26
Swagger What’s problem : API 명세서 관리 프로젝트를 하면서 불편한 점을 하나씩 해결해보고자 한다. 이번에는 API 명세서를 작성하며 느낀 불편함을 개선해보기로 했다. 보통 프로젝트를 시작할 때 API를 개발하기 전에 명세서를 먼저 작성하여 프론트엔드 팀원에게 공유하고, API 개발이 끝나면 postman을 활용해 API 명세를 공유하고 있었다. Gitbook으로 직접 손으로 작성하는 것 보다는 postman이 편했지만, postman으로 API 명세서를 작성하다 보니 API가 어떻게 동작하는지에 대한 자세한 설명을 기입할 수가 없고 크고 작은 수정이 발생하면 직접 해당 API를 postman 통신하여 예외처리한 부분들까지 적용해주는 과정이 꽤 번거로웠다. How solve the ..
-
[Nest.js 공식문서 읽기] MiddlewareServer/Nest.js 2023. 1. 27. 15:04
Middleware 미들웨어란 라우트핸들러 전에 호출되는 함수이다. 미들웨어 함수는 req, res 객체에 접근 가능하며 애플리케이션의 요청-응답 주기에서 next() 미들웨어 함수에 접근할 수 있다. Nest 미들웨어는 express의 미들웨어와 같다. 📌 Official express documentation describes the capabilities of middleware. Middleware functions can perform the following tasks: execute any code. make changes to the request and the response objects. end the request-response cycle. call the next middlewa..
-
[Nest.js 공식문서 읽기] ProviderServer/Nest.js 2023. 1. 27. 15:02
Provider provider는 nest의 기본개념이다. nest의 services, repositories, factories, helpers등 기본 클래스들은 대부분 provider로 취급될 수 있다. 그리고 provider는 종속성으로 주입될 수 있다. 이것의 의미는 객체들이 다양한 관계를 만들 수 있다는 것을 의미하고, 그 관계 설정에 맞게 인스턴스를 생성하는 일은 nest 런타임에게 위임된다. 물론 provider도 controller처럼 module에 메타데이터를 전달해주어야 nest가 해당 클래스를 provider로 인식할 수 있다. 📌 SOLID(객체 지향 설계) nest로 객체 지향 방식의 종속성을 설계하고 구상할 수 있기 때문에 SOLID 원칙을 따르는 것을 강력히 권고한다. The ..
-
[Nest.js 공식문서 읽기] ModuleServer/Nest.js 2023. 1. 27. 14:58
Module module은 만들어진 controller, sevice, repository 등을 하나로 합치는 역할을 한다. 캡슐화 nest의 모듈은 기본적으로 provider를 캡슐화한다. 즉, 현재 모듈에서 export된 프로바이더나 주입된 모듈에서 export된 프로바이더가 아니라면 프로바이더를 주입(inject)하는 것은 불가능하다. 만약 하나의 모듈에서 직접 exports를 통해 내보낸 것들이 아니면 외부에서 접근 또는 삽입이 불가능하다. 예를들어 a, b, c 모듈이 있다고 가정하면 a 모듈에서 b 모듈의 프로바이더를 사용하고 싶다면, b 모듈에서 해당 프로바이더를 직접 exports에 등록해주어야 한다. export에 등록하는 방법은 @Module 데코레이터 내부에 등록하면 된다. 위 예시처..
-
[Nest.js 공식문서 읽기] ControllerServer/Nest.js 2023. 1. 27. 14:57
컨트롤러 컨트롤러는 애플리케이션에 대한 특정 요청을 받고 응답을 리턴하는 곳이다. 라우팅 express에서는 라우터와 컨트롤러를 분리했었는데, nest는 express의 라우터와 컨트롤러를 합쳐놓은 모습이었다. nest에서는 express에서 사용하지 않던 데코레이터라는 것을 사용한다. 📌 데코레이터 데코레이터는 클래스의 필수 메타데이터를 등록하고 nest가 라우팅 맵을 생성할 수 있도록 한다. Nest에서 제공하는 데코레이터를 사용할 경우 자체적으로 인스턴스를 생성해준다. - Nest에서는 불필요한 메모리 사용을 방지하기 위하여 싱글톤 패턴을 적용시킬 수 있는 데코레이터를 제공한다. - @Resolver(for GraphQL), @Controller(), @injectable() 등, 해당하는 것을 모..