어차피 백엔드 배우고 싶었음. 오히려 좋아 럭키비키쟈나
프로젝트 만드는 것부터 차근차근 시작해 보겠슴.
참고) 구조 이해를 프론트적 사고로 할 수 있음 주의
$ npm install -g @nestjs/cli
$ nest new my-nest-project
$ cd my-nest-project
$ npm run start:dev
이렇게 하면 프로젝트는 성공
구조를 간단히 설명하면
import { NestFactory } from '@nestjs/core';
import { AppModule } from './app.module';
async function bootstrap() {
const app = await NestFactory.create(AppModule);
await app.listen(process.env.PORT ?? 3000);
}
bootstrap();
참고) NestFactory
import { Module } from '@nestjs/common';
import { AppController } from './app.controller';
import { AppService } from './app.service';
import { UsersModule } from './users/users.module';
@Module({
imports: [],
controllers: [AppController],
providers: [AppService],
})
export class AppModule {}
import { Controller, Get } from '@nestjs/common';
import { AppService } from './app.service';
@Controller()
export class AppController {
constructor(private readonly appService: AppService) {}
@Get()
getHello(): string {
return this.appService.getHello();
}
}
@Controller()
데코레이터를 호출: 컨트롤러로 인식!@Get()
, @Post()
, @Delete()
와 같은 HTTP 방식(method)에 해당하는 데코레이터를 명시해서 사용import { Injectable } from '@nestjs/common';
@Injectable()
export class AppService {
getHello(): string {
return 'Hello World!';
}
}
@Injectable()
데코레이터: 다른 클래스에 생성자를 통해서 주입해줄 수 있음 Nest의 핵심일지도!
Controller는 Service를 의존한다.
컨트롤러는 서비스에서 함수를 가져다 쓰기 때문
에 의존한다고 봄 - 따라서 서비스가 변하면 컨트롤러도 영향을 받음@Controller('pur')
export class PurController {
constructor(private readonly purServices: PurService) {}
}
constructor
내부에 선언그럼 일단 이 정도하고, 설치가 잘 됐는지 확인부터 해보자!
응답 값을 잘 받아오고 있음을 확인!
nest g module users
import { Module } from '@nestjs/common';
@Module({})
export class UsersModule {}
import { Module } from '@nestjs/common';
import { AppController } from './app.controller';
import { AppService } from './app.service';
import { UsersModule } from './users/users.module';
@Module({
imports: [UsersModule],
controllers: [AppController],
providers: [AppService],
})
export class AppModule {}
nest g controller users
nest g service users
컨트롤러와 서비스도 다 만들 수 있음!
싱기방기 뿡뿡방기........
import { Module } from '@nestjs/common';
import { UsersController } from './users.controller';
import { UsersService } from './users.service';
@Module({
controllers: [UsersController],
providers: [UsersService],
})
export class UsersModule {}
npm run start
# Lint and autofix with eslint
$ npm run lint
# Format with prettier
$ npm run format
언니 드디어 백엔드에 발을 들였구나 ^_^ 빵끗 웃으면서 들어왔자나 ^^77