Nest.js 얼렁뚱땅 게시판 #1

Nest Next 뭐 만만한 이름은 죄다 있는 이러한 JS 기반의 인프라를 처음 접하게 되었다. 어차피 여러분들은 핵심적인 것은 GPT 내지 나름의 책을 쓰다가 이 글에 들어왔다는건 거기에 잘 안나오는 내용일 것이다. 그래서 내가 겪은 흐름을 적어본다.


NodeJS와 TypeScript 기본적으로 설치해준다음 Nest.JS 에 들어가면 npm 명령어에 기반한 자기네들 라이브러리 설치 링크가 있다.

https://docs.nestjs.com/first-steps

 

Documentation | NestJS - A progressive Node.js framework

Nest is a framework for building efficient, scalable Node.js server-side applications. It uses progressive JavaScript, is built with TypeScript and combines elements of OOP (Object Oriented Programming), FP (Functional Programming), and FRP (Functional Rea

docs.nestjs.com

 

여기서 하라는대로하면 직접 이름 지을 수 있는 프로젝트를 만들 수 있다. 해당 프로젝트를 VSCode로 열어보자.

dist, node_modules는 당장 건드릴게 없다.

그리고 터미널을 통해 이 경로에서 npm run start:dev 하면 localhost:3000 에서 Hello World를 볼 수 있다.

 

터미널을 현재 Nest에 대해 작성하고 있는 프로젝트로 초점을 이동시킨다음 다음 세 명령어를 차례로 쳤다.

nest generate module board
nest generate controller board
nest generate service board

기본 템플릿들이고, 이제 src 폴더 안에 board라는 폴더가 만들어졌을 것이다. 아마 5개가 있다.

board.entity.ts라는 파일을 새로 만들자. board 폴더 안에다 만들고, 곧 바로 내용을 다음과 같이 썼다.

참고로 워.싱.시.ts 같은 이름 규칙은 암묵적 룰이다. 개인 프로젝트를 한다면 kim.chi.good.ts 같은걸로 해보길 기대한다.

import { Entity, Column, PrimaryGeneratedColumn, CreateDateColumn, UpdateDateColumn } from 'typeorm';

@Entity()
export class Board {
  @PrimaryGeneratedColumn()
  id: number;

  @Column()
  title: string;

  @Column('text')
  content: string;

  @Column()
  author: string;

  @CreateDateColumn()
  createdAt: Date;

  @UpdateDateColumn()
  updatedAt: Date;
}

여기서 TypeORM 라이브러리를 찾을 수 없는 문제를 겪어서 프로젝트 경로에서 새롭게 설치하였다.

# TypeORM 관련 패키지 설치
npm install @nestjs/typeorm typeorm

 

이제 DTO를 작성해야한다. DTO는 데이터의 구조를 정의하고 검증하는 역할로, 잘못된 형태의 데이터 들어옴을 방지한다.

이전에 있었던 src/board 폴더 내에 또 dto라는 폴더를 만든다. 표준적 위치는 여기라고 한다.

create-board.dto.ts 를 만든다.

export class CreateBoardDto {
  title: string;
  content: string;
  author: string;
}

 

update-board.dto.ts 를 만든다.

import { PartialType } from '@nestjs/mapped-types';
import { CreateBoardDto } from './create-board.dto';

export class UpdateBoardDto extends PartialType(CreateBoardDto) {}

여기서 '@nestjs/mapped-types'; 관련 문제가 있어서 아래 내용을 또 설치하였다. 왜 기본으로 안깔아주는거야

npm install @nestjs/mapped-types

이정도만 하니 오류로써 뜨는 빨간 줄은 왠만하게 해결이 이루어졌다.

 

다음 내용은 service 구현 로직으로 넘어가겠다.

 

'with Nest' 카테고리의 다른 글

Nest.js 얼렁뚱땅 게시판 #3  (4) 2025.06.16
TypeORM  (0) 2025.06.16
Nest.js 얼렁뚱땅 게시판 #2  (2) 2025.06.16