본문 바로가기
카테고리 없음

NestJS로 대규모 백엔드 서비스 구축하기

by 티끌코딩 2025. 3. 3.
반응형
NestJS로 대규모 백엔드 서비스 구축하기

NestJS는 Node.js 기반의 강력한 백엔드 프레임워크로, 확장성과 모듈성을 갖춘 아키텍처를 제공합니다.

1. NestJS란?

NestJS는 TypeScript 기반의 백엔드 프레임워크로, Angular의 철학을 따르는 모듈형 구조를 제공합니다.

  • Node.js 기반, Express 또는 Fastify와 함께 사용 가능
  • 타입 안정성이 높은 TypeScript 지원
  • 모듈 기반 아키텍처 제공

공식 사이트: NestJS

2. NestJS 프로젝트 생성 및 설정

다음 명령어로 NestJS 프로젝트를 생성합니다.


        npm i -g @nestjs/cli
        nest new my-nest-app
        cd my-nest-app
        npm run start
        

이제 http://localhost:3000에서 기본 애플리케이션을 실행할 수 있습니다.

3. 컨트롤러 및 서비스 생성

NestJS에서는 컨트롤러와 서비스를 분리하여 코드를 구조화합니다.

컨트롤러 생성


        nest generate controller users
        

서비스 생성


        nest generate service users
        

컨트롤러 코드 예제


        import { Controller, Get } from '@nestjs/common';
        import { UsersService } from './users.service';

        @Controller('users')
        export class UsersController {
            constructor(private readonly usersService: UsersService) {}

            @Get()
            findAll() {
                return this.usersService.findAll();
            }
        }
        

서비스 코드 예제


        import { Injectable } from '@nestjs/common';

        @Injectable()
        export class UsersService {
            private users = [{ id: 1, name: '홍길동' }];

            findAll() {
                return this.users;
            }
        }
        

4. 데이터베이스 연동 (PostgreSQL)

TypeORM을 사용하여 PostgreSQL과 연결할 수 있습니다.


        npm install @nestjs/typeorm typeorm pg
        

app.module.ts에서 설정 추가:


        import { Module } from '@nestjs/common';
        import { TypeOrmModule } from '@nestjs/typeorm';

        @Module({
            imports: [
                TypeOrmModule.forRoot({
                    type: 'postgres',
                    host: 'localhost',
                    port: 5432,
                    username: 'user',
                    password: 'password',
                    database: 'mydb',
                    autoLoadEntities: true,
                    synchronize: true,
                }),
            ],
        })
        export class AppModule {}
        

5. 인증 및 보안 설정 (JWT)

JWT(JSON Web Token)를 사용하여 인증 시스템을 구현합니다.


        npm install @nestjs/jwt passport-jwt
        

이후 Auth 모듈을 생성하여 인증 로직을 추가할 수 있습니다.

6. NestJS의 장점 및 대규모 서비스 적용

  • 모듈 기반 아키텍처로 확장성이 뛰어남
  • GraphQL, WebSockets 지원
  • 대규모 애플리케이션 구조화에 적합

7. 결론

NestJS는 백엔드 개발을 효율적으로 수행할 수 있도록 돕는 강력한 프레임워크입니다. 대규모 애플리케이션 개발을 위해 NestJS를 활용해보세요.

반응형