내일배움캠프 노드 4기/Today I Learned (62) 썸네일형 리스트형 [Nest.js][TypeORM] 시드 만들기 [Nest.js] 미들웨어 사용해보기 auth.middleware.ts .... @Injectable() export class AuthMiddleware implements NestMiddleware { constructor( private readonly jwtService: JwtService, @Inject(CACHE_MANAGER) private readonly cacheManager: Cache, ) {} async use(req: any, res: any, next: (error?: any) => void) { const { accessToken, refreshToken } = req.cookies; ... 먼저 미들웨어를 작성합니다 // 프로젝트에서 사용한 인증 미들웨어 app.module.ts import { AuthMidd.. [Nest.js] lodash 모듈을 못 불러와요 현상 [Nest] 3064 - 2023. 02. 21. 오전 2:05:06 ERROR [ExceptionsHandler] Cannot read properties of undefined ( reading 'isNil') lodash의 isNil 프로퍼티를 사용할수가 없다 해결 tsconfig.json에 코드를 추가해준다 { "compilerOptions": { // ... "esModuleInterop": true // ... } } https://velog.io/@mskwon/import-as-from-lodash%EB%8A%94-%EB%90%98%EB%8A%94%EB%8D%B0-import-from-lodash-%EC%99%9C-%EC%95%88%EB%90%A8 import * as _ from 'l.. [Nest.js] DTO Nest.js에서 클라이언트와 데이터 통신을 하기 위해서는 DTO(Data Transfer Object)를 사용해야 합니다. DTO는 데이터를 전송하기 위해 작성된 객체로 Nest.js에서는 모든 데이터를 DTO로 주고받습니다. DTO를 작성하기 위한 패키지 설치 $ npm i class-validator class-transformer DTO 클래스 작성 // create-article.dto.ts import { IsNumber, IsString } from 'class-validator'; export class CreateArticleDto { @IsString() readonly title: string; @IsString() readonly content: string; @IsNumber() .. [Nest.js] 게시판 만들기 새로운 모듈 만들기 $ nest g mo board src 폴더 내에 board 폴더가 자동으로 생성되고 board.module.ts 라는 타입스크립트 코드가 생성되어있다. // board.module.ts import { Module } from '@nestjs/common'; @Module({}) export class BoardModule {} 컨트롤러 생성 $ nest g co board 모듈을 생성할 때와 같이 board 폴더에 코드가 생성된다. // board.controller.ts import { Controller } from '@nestjs/common'; // Controller 데코레이터 옆에 문자열은 라우팅 룰이라고 생각하시면 됩니다! // 이 컨트롤러는 /board 라는 주소 아.. [Nest.js] IoC와 DI IoC (Inversion of Control) : 제어 역전 기존에는 어떤 서비스를 사용하고 싶으면 아래와 같은 코드를 직접 작성해야 했다. import { Controller, Get } from '@nestjs/common'; import { AppService } from './app.service'; @Controller() export class AppController { // 1. 사용하고 싶은 서비스 타입 객체를 미리 선언합니다. private appService: AppService constructor() { // 2. 생성자에서 실제로 사용할 서비스 객체를 직접 생성합니다. this.appService = new AppService(); } ... } 어떤 객체를 사용하고 싶을 때 .. [Nest.js] 프로젝트 기본 코드 분석 1. main.ts main.ts는 Nest.js에서 진입점으로 사용하겠다고 사전에 약속된 파일이므로 임의로 파일명을 변경해서는 안된다.(중요) import { NestFactory } from '@nestjs/core'; import { AppModule } from './app.module'; async function bootstrap() { const app = await NestFactory.create(AppModule); await app.listen(3000); } bootstrap(); Nest.js 웹 어플리케이션의 진입점으로 NestFactory 클래스의 create라는 정적 함수를 통해 Nest.js 어플리케이션 인스턴스를 새롭게 생성한다. const app = await NestF.. [Nest.js] 설치하기 터미널에 입력해서 설치 npm i -g @nestjs/cli nest 명령어 보기 nest 모르는 명령어가 있을 때 입력해서 확인할 수 있다. 새로운 프로젝트 만들기 // nest n "프로젝트명" nest new sparta-nest 입력하면 아래와 같이 나오는데 Nest.js와 가장 호환이 잘 되는 npm에서 Enter 키를 누르면 된다. ? Which package manager would you ❤️ to use? (Use arrow keys) ❯ npm yarn pnpm 이전 1 2 3 4 ··· 8 다음