1. ์ ํ๋ก์ ํธ ์์ฑ
1nest new .2# yarn ์ ํ
2. ํฌ์คํธ JSON ๋ฐํ
app.controller.ts๋ฅผ ์์
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๋ฅผ ๋ฃ์ ์ ์๋ค.
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 ๋ฐ์ฝ๋ ์ดํฐ์๋ค๊ฐ ๋ฃ์ ์๋ ์๋ค.
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}
๋ง์ฝ ๋ฐ์ฝ๋ ์ดํฐ ๋ ๋ค ๊ฒฝ๋ก๋ฅผ ๋ฃ์ผ๋ฉด, ๊ฒฝ๋ก/๊ฒฝ๋ก๋ก ๋ค์ด๊ฐ์ผ ๋๋ค.
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 ๋ชจ๋ ์์ฑ
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์ผ๋ก ์์ฒญ๋ณด๋ด๊ณ ํ์ธํ์ธ์.