02-Controller
1. 새 프로젝트 생성
shellscript
1nest new .2# yarn 선택
2. 포스트 JSON 반환
app.controller.ts를 수정
typescript
1import { Controller, Get } from '@nestjs/common'2import { AppService } from './app.service'34interface Post {5author: string6title: string7content: string8likeCount: number9commentCount: number10}1112@Controller()13export class AppController {14constructor(private readonly appService: AppService) {}1516@Get()17getPost(): Post {18return {19author: 'newjeans_official',20title: '뉴진스 민지',21content: '메이크업 고치고 있는 민지',22likeCount: 100000,23commentCount: 999999,24}25}26}
3. 데코레이터에 Path 추가
Controller 데코레이터에다가 path를 넣을 수 있다.
typescript
1import { Controller, Get } from '@nestjs/common'2import { AppService } from './app.service'34interface Post {5author: string6title: string7content: string8likeCount: number9commentCount: number10}1112@Controller('post') // 데코레이터에 path 추가13export class AppController {14constructor(private readonly appService: AppService) {}1516@Get()17getPost(): Post {18return {19author: 'newjeans_official',20title: '뉴진스 민지',21content: '메이크업 고치고 있는 민지',22likeCount: 100000,23commentCount: 999999,24}25}26}
또 다른 방법으로 get 데코레이터에다가 넣을 수도 있다.
typescript
1import { Controller, Get } from '@nestjs/common'2import { AppService } from './app.service'34interface Post {5author: string6title: string7content: string8likeCount: number9commentCount: number10}1112@Controller()13export class AppController {14constructor(private readonly appService: AppService) {}1516@Get('post') // 데코레이터에 path 추가17getPost(): Post {18return {19author: 'newjeans_official',20title: '뉴진스 민지',21content: '메이크업 고치고 있는 민지',22likeCount: 100000,23commentCount: 999999,24}25}26}
만약 데코레이터 둘 다 경로를 넣으면, 경로/경로로 들어가야 된다.
typescript
1import { Controller, Get } from '@nestjs/common'2import { AppService } from './app.service'34interface Post {5author: string6title: string7content: string8likeCount: number9commentCount: number10}1112@Controller('post') // 데코레이터에 path 추가13export class AppController {14constructor(private readonly appService: AppService) {}1516@Get('post') // 데코레이터에 path 추가17getPost(): Post {18return {19author: 'newjeans_official',20title: '뉴진스 민지',21content: '메이크업 고치고 있는 민지',22likeCount: 100000,23commentCount: 999999,24}25}26}
위 코드의 경우에는 post/post로 들어가야 한다.
- 왜나하면
controller 데코레이터를 붙여준 클래스 안의 모든 엔드포인트들이 path 접두어를 갖고, - 그 이후에
get 데코레이터를 붙인 함수에 엔드포인트들이 path 접두어를 갖기 때문이다.
4. posts 모듈 생성
shellscript
1# g는 generate의 약자2nest g resource34? What name would you like to use for this resource (plural, e.g., "users")?5posts6? What transport layer do you use? REST API7? Would you like to generate CRUD entry points? No
그러면 posts 폴더와 그 안의 폴더들이 자동으로 만들어집니다.
그 후 posts.controller.ts의 코드를 다음과 같이 수정하고,
app.controller.ts 코드는 원상복귀시킵니다.
posts.controller.ts
1import { Controller, Get } from '@nestjs/common'2import { PostsService } from './posts.service'34interface Post {5author: string6title: string7content: string8likeCount: number9commentCount: number10}1112@Controller('posts')13export class PostsController {14constructor(private readonly postsService: PostsService) {}1516@Get()17getPost(): Post {18return {19author: 'newjeans_official',20title: '뉴진스 민지',21content: '메이크업 고치고 있는 민지',22likeCount: 100000,23commentCount: 999999,24}25}26}
app.controller.ts
1import { Controller } from '@nestjs/common'2import { AppService } from './app.service'34@Controller()5export class AppController {6constructor(private readonly appService: AppService) {}7}
Postman으로 요청보내고 확인하세요.