๐ŸŽ‰ berenickt ๋ธ”๋กœ๊ทธ์— ์˜จ ๊ฑธ ํ™˜์˜ํ•ฉ๋‹ˆ๋‹ค. ๐ŸŽ‰
Back
NestJs
02-Controller

1. ์ƒˆ ํ”„๋กœ์ ํŠธ ์ƒ์„ฑ

1
nest new .
2
# yarn ์„ ํƒ

2. ํฌ์ŠคํŠธ JSON ๋ฐ˜ํ™˜

app.controller.ts๋ฅผ ์ˆ˜์ •

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๋ฅผ ๋„ฃ์„ ์ˆ˜ ์žˆ๋‹ค.

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 ๋ฐ์ฝ”๋ ˆ์ดํ„ฐ์—๋‹ค๊ฐ€ ๋„ฃ์„ ์ˆ˜๋„ ์žˆ๋‹ค.

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
}

๋งŒ์•ฝ ๋ฐ์ฝ”๋ ˆ์ดํ„ฐ ๋‘˜ ๋‹ค ๊ฒฝ๋กœ๋ฅผ ๋„ฃ์œผ๋ฉด, ๊ฒฝ๋กœ/๊ฒฝ๋กœ๋กœ ๋“ค์–ด๊ฐ€์•ผ ๋œ๋‹ค.

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 ๋ชจ๋“ˆ ์ƒ์„ฑ

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์œผ๋กœ ์š”์ฒญ๋ณด๋‚ด๊ณ  ํ™•์ธํ•˜์„ธ์š”.