Skip to Content
BackendNestJs02-Controller

1. 새 프로젝트 생성

shellscript
1
nest new .
2
# yarn 선택

2. 포스트 JSON 반환

app.controller.ts를 수정

typescript
1
import { Controller, Get } from '@nestjs/common'
2
import { AppService } from './app.service'
3
4
interface Post {
5
author: string
6
title: string
7
content: string
8
likeCount: number
9
commentCount: number
10
}
11
12
@Controller()
13
export class AppController {
14
constructor(private readonly appService: AppService) {}
15
16
@Get()
17
getPost(): Post {
18
return {
19
author: 'newjeans_official',
20
title: '뉴진스 민지',
21
content: '메이크업 고치고 있는 민지',
22
likeCount: 100000,
23
commentCount: 999999,
24
}
25
}
26
}

3. 데코레이터에 Path 추가

Controller 데코레이터에다가 path를 넣을 수 있다.

typescript
1
import { Controller, Get } from '@nestjs/common'
2
import { AppService } from './app.service'
3
4
interface Post {
5
author: string
6
title: string
7
content: string
8
likeCount: number
9
commentCount: number
10
}
11
12
@Controller('post') // 데코레이터에 path 추가
13
export class AppController {
14
constructor(private readonly appService: AppService) {}
15
16
@Get()
17
getPost(): Post {
18
return {
19
author: 'newjeans_official',
20
title: '뉴진스 민지',
21
content: '메이크업 고치고 있는 민지',
22
likeCount: 100000,
23
commentCount: 999999,
24
}
25
}
26
}

또 다른 방법으로 get 데코레이터에다가 넣을 수도 있다.

typescript
1
import { Controller, Get } from '@nestjs/common'
2
import { AppService } from './app.service'
3
4
interface Post {
5
author: string
6
title: string
7
content: string
8
likeCount: number
9
commentCount: number
10
}
11
12
@Controller()
13
export class AppController {
14
constructor(private readonly appService: AppService) {}
15
16
@Get('post') // 데코레이터에 path 추가
17
getPost(): Post {
18
return {
19
author: 'newjeans_official',
20
title: '뉴진스 민지',
21
content: '메이크업 고치고 있는 민지',
22
likeCount: 100000,
23
commentCount: 999999,
24
}
25
}
26
}

만약 데코레이터 둘 다 경로를 넣으면, 경로/경로로 들어가야 된다.

typescript
1
import { Controller, Get } from '@nestjs/common'
2
import { AppService } from './app.service'
3
4
interface Post {
5
author: string
6
title: string
7
content: string
8
likeCount: number
9
commentCount: number
10
}
11
12
@Controller('post') // 데코레이터에 path 추가
13
export class AppController {
14
constructor(private readonly appService: AppService) {}
15
16
@Get('post') // 데코레이터에 path 추가
17
getPost(): Post {
18
return {
19
author: 'newjeans_official',
20
title: '뉴진스 민지',
21
content: '메이크업 고치고 있는 민지',
22
likeCount: 100000,
23
commentCount: 999999,
24
}
25
}
26
}

위 코드의 경우에는 post/post로 들어가야 한다.

  • 왜나하면 controller 데코레이터를 붙여준 클래스 안의 모든 엔드포인트들이 path 접두어를 갖고,
  • 그 이후에 get 데코레이터를 붙인 함수에 엔드포인트들이 path 접두어를 갖기 때문이다.

4. posts 모듈 생성

shellscript
1
# g는 generate의 약자
2
nest g resource
3
4
? What name would you like to use for this resource (plural, e.g., "users")?
5
posts
6
? What transport layer do you use? REST API
7
? Would you like to generate CRUD entry points? No

그러면 posts 폴더와 그 안의 폴더들이 자동으로 만들어집니다.

그 후 posts.controller.ts의 코드를 다음과 같이 수정하고, app.controller.ts 코드는 원상복귀시킵니다.

posts.controller.ts
1
import { Controller, Get } from '@nestjs/common'
2
import { PostsService } from './posts.service'
3
4
interface Post {
5
author: string
6
title: string
7
content: string
8
likeCount: number
9
commentCount: number
10
}
11
12
@Controller('posts')
13
export class PostsController {
14
constructor(private readonly postsService: PostsService) {}
15
16
@Get()
17
getPost(): Post {
18
return {
19
author: 'newjeans_official',
20
title: '뉴진스 민지',
21
content: '메이크업 고치고 있는 민지',
22
likeCount: 100000,
23
commentCount: 999999,
24
}
25
}
26
}
app.controller.ts
1
import { Controller } from '@nestjs/common'
2
import { AppService } from './app.service'
3
4
@Controller()
5
export class AppController {
6
constructor(private readonly appService: AppService) {}
7
}

Postman으로 요청보내고 확인하세요.