๐ Nest์ ์ปจํธ๋กค๋ฌ = MVCํจํด์ ์ปจํธ๋กค๋ฌ
์ปจํธ๋กค๋ฌ๋ ๋ค์ด์ค๋ request๋ฅผ ๋ฐ๊ณ ์ฒ๋ฆฌ๋ ๊ฒฐ๊ณผ๋ฅผ response๋ก ๋๋ ค์ฃผ๋ ์ธํฐํ์ด์ค ์ญํ
์์ค์ฝ๋๋ฅผ ์ง์ ์์ฑํด๋ ๋๋ค!
nest g controller [name] #๋๋ testing์ด๋ผ๊ณ ํด๋จ์
์๋ ์์ฑ๋ ์ฝ๋๋ฅผ ๋ณด๋ฉด AppController
๋AppModule
์ ์ ์ธ๋์ด ์๊ณ , AppService
๋ฅผ importํด์ ์ฌ์ฉ ์ค
// ./app.Controllers.ts
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();
}
}
nest -h
: Nest ๊ตฌ์ฑ์์ ๋ณด๊ธฐ
nest g resource [name] //Users๋ผ๊ณ ์์ฑ
// ./app.Controllers.ts
import { Controller, Get } from '@nestjs/common';
import { AppService } from './app.service';
@Controller() //๋ฐ์ฝ๋ ์ดํฐ๋ฅผ ํด๋์ค์ ์ ์ธ : AppController ํด๋์ค๋ ์ปจํธ๋กค๋ฌ์ผ ์ญํ ์ ํ๊ฒ๋๋ค
export class AppController {
constructor(private readonly appService: AppService) {}
@Get() //๋ฃจํธ๊ฒฝ๋ก๋ก ๋ค์ด์ค๋ ์์ฒญ ์ฒ๋ฆฌ ๊ฐ๋ฅ
getHello(): string {
return this.appService.getHello();
}
}
// ./app.Controllers.ts
import { Controller, Get } from '@nestjs/common';
import { AppService } from './app.service';
@Controller() //๋ฐ์ฝ๋ ์ดํฐ๋ฅผ ํด๋์ค์ ์ ์ธ : AppController ํด๋์ค๋ ์ปจํธ๋กค๋ฌ์ผ ์ญํ ์ ํ๊ฒ๋๋ค
export class AppController {
constructor(private readonly appService: AppService) {}
@Get('/hello') //๋ฃจํธ๊ฒฝ๋ก์์ /hello๋ก ๋ณ๊ฒฝ
getHello(): string {// getHello๋ returnHello๋ ์๊ด์์(์์๋์ด์๋ ๋ด๊ท๋ฅผ ๋ฐ๋ฅด๋ฉด ๋๋ค.)
return this.appService.getHello();
}
}
๋น์ฐํ๊ฒ ์ง๋ง ๋ฃจํธ๊ฒฝ๋ก์์ ์๋ฌ๋ฐํ
/hello
์์ ๊ฒฐ๊ณผ response
// ./app.Controllers.ts
import { Controller, Get } from '@nestjs/common';
import { AppService } from './app.service';
@Controller() //๋ฐ์ฝ๋ ์ดํฐ๋ฅผ ํด๋์ค์ ์ ์ธ : AppController ํด๋์ค๋ ์ปจํธ๋กค๋ฌ์ผ ์ญํ ์ ํ๊ฒ๋๋ค
export class AppController {
constructor(private readonly appService: AppService) {}
@Get('he*lo') // ๋ฌธ์์ด ๊ฐ์ด๋ฐ ์ด๋ค ๋ฌธ์๊ฐ ์๋ ์๊ด์์ด ๋ผ์ฐ๋ง ํจ์ค ๊ตฌ์ฑ
getHello(): string {// getHello๋ returnHello๋ ์๊ด์์(์์๋์ด์๋ ๋ด๊ท๋ฅผ ๋ฐ๋ฅด๋ฉด ๋๋ค.)
return this.appService.getHello();
}
}
*
๋ง๊ณ ๋ ?
,+
,(
,)
๋ํ ์ ๊ท ํํ์์์์ ์์ผ๋ ์นด๋์ ๋์ผํ๊ฒ ๋์-
,.
์ ๋ฌธ์์ด๋ก ์ทจ๊ธํ๊ธฐ ๋๋ฌธ์ ์ ์ธNest๋ ์๋ต ํค๋๋ ์๋์ผ๋ก ๊ตฌ์ฑํด์ค๋ค.
์๋ต์ ์ปค์คํ
ํค๋๋ฅผ ์ถ๊ฐํ๊ณ ์ถ๋ค๋ฉด @Header
๋ฐ์ฝ๋ ์ดํฐ๋ฅผ ์ฌ์ฉํ๋ฉด ๋๋ค.์ธ์๋ ํค๋์ด๋ฆ๊ณผ ๊ฐ
์๋๋ฉด ๋ผ์ด๋ธ๋ฌ๋ฆฌ์์ ์ ๊ณตํ๋ ์๋ต๊ฐ์ฒด๋ฅผ ์ฌ์ฉํ์ฌ res.header() ๋ฉ์๋๋ก ์ง์ ์ค์ ๋ ๊ฐ๋ฅ
ex)
import { Header } from '@nestjs/common';
@Header('Custom', 'jiminnote')
@Get(':id')
findOneWithHeader(@Param('id') id: string) {
return this.usersService.findOne(+id);
}
< Custom: jiminnote
์ด๋ ๊ฒ ๋์ด์์ ๊ฒ ํ์ธ ๊ฐ๋ฅ
๊ฐ๋ ์๋ฒ๋ ์์ฒญ์ ์ฒ๋ฆฌํ ํ ์์ฒญ์ ๋ณด๋ธ ํด๋ผ์ด์ธํธ๋ฅผ ๋ค๋ฅธ ํ์ด์ง๋ก ์ด๋์ํค๊ณ ์ถ์ ๊ฒฝ์ฐ๊ฐ ์๋๋ฐ ์ด๋!
1. ์๋ด ๋ณธ๋ฌธ์ `redirectUrl์ ํฌํจ์์ผ ํด๋ผ์ด์ธํธ๊ฐ ์ค์ค๋ก ํ์ด์ง๋ฅผ ์ด๋ 2.
@Redirect`๋ฐ์ฝ๋ ์ดํฐ๋ฅผ ์ฌ์ฉํ์ฌ ๊ตฌํ
301 Moved Permanatly๋ ์์ฒญํ ๋ฆฌ์์ค๊ฐ ํค๋์ ์ฃผ์ด์ง ๋ฆฌ์์ค๋ก ์์ ํ ์ด๋๋๋ค๋ ๋ป
import { Redirect } from '@nestjs/common';
@Redirect('https://nestjs.com', 301)
@Get(':id')
findOne(@Param('id') id: string) {
return this.usersService.findOne(+id);
}
์ฟผ๋ฆฌ ํ๋ผ๋ฏธํฐ๋ก ๋ฒ์ ์ซ์๋ฅผ ์ ๋ฌ๋ฐ์ ํด๋น ๋ฒ์ ์ ํ์ด์ง๋ก ์ด๋ํ๋ค
@Get('redirect/docs')
@Redirect('https://docs.nestjs.com', 302)
getDocs(@Query('version') version) {
if (version && version === '5') {
return { url: 'https://docs.nestjs.com/v5/' };
}
}
=> http://localhost:3000/redirect/docs?version=5
์
๋ ฅ์
https://docs.nestjs.com/v5/
๋ก ์ด๋
๐ ๋ผ์ฐํธ ํ๋ผ๋ฏธํฐ๋ฅผ ์ ๋ฌ๋ฐ๋ ๋ฐฉ๋ฒ
1. ํ๋ผ๋ฏธํฐ๊ฐ ์ฌ๋ฌ ๊ฐ ์ ๋ฌ๋ ๊ฒฝ์ฐ ๊ฐ์ฒด๋ก ํ๋ฒ์ ๋ฐ๊ธฐ
(paramsํ์
์ด any๊ฐ ๋์ ๋๋๋ก ์ฌ์ฉ X ์๋๋ฉด ๋ผ์ฐํธ ํ๋ผ๋ฏธํฐ๋ ํ์
์ด ํญ์ string์ด๊ธฐ ๋๋ฌธ์ ๋ช
์์ ์ผ๋ก { [key: string]: string } ํ์
์ ์ง์ )
@Delete(':userId/memo/:memoId')
deleteUserMemo(@Param() params: { [key: string]: string }) {
return `userId: ${params.userId}, memoId: ${params.memoId}`;
}
@Delete(':userId/memo/:memoId')
deleteUserMemo(
@Param('userId') userId: string,
@Param('memoId') memoId: string,
) {
return `userId: ${userId}, memoId: ${memoId}`;
}