뭔가를 원한다면 요청해야한다
@Post 리퀘스트의 body 부분을 가져오고 싶다고 하자
무비를 생성할때 body를 가져오고 싶을때
@Post()
create() {
return "This will create a movie";
}
현재 상태에서 body도 가져오고 싶을때
{
"name": "Tenet",
"director": "kon"
}
—
@Post()
create(@Body() movieData) {
console.log(movieData);
return "This will create a movie";
}
@Body : movieData 데이터를 가져오기 위함
[Nest] 6321 - 09/09/2024, 11:12:22 PM LOG [RouterExplorer] Mapped {/movies/:id, DELETE} route +0ms
[Nest] 6321 - 09/09/2024, 11:12:22 PM LOG [RouterExplorer] Mapped {/movies/:id, PATCH} route +1ms
[Nest] 6321 - 09/09/2024, 11:12:22 PM LOG [NestApplication] Nest application successfully started +0ms
{
query: '\n' +
' query IntrospectionQuery {\n' +
' schema {\n' +
' \n' +
' queryType { name }\n' +
' mutationType { name }\n' +
' subscriptionType { name }\n' +
' types {\n' +
' ...FullType\n' +
' }\n' +
' directives {\n' +
' name\n' +
' description\n' +
' \n' +
' locations\n' +
' args {\n' +
' ...InputValue\n' +
' }\n' +
' }\n' +
' }\n' +
' }\n' +
'\n' +
' fragment FullType on Type {\n' +
' kind\n' +
' name\n' +
' description\n' +
' \n' +
' fields(includeDeprecated: true) {\n' +
' name\n' +
' description\n' +
' args {\n' +
' ...InputValue\n' +
' }\n' +
' type {\n' +
' ...TypeRef\n' +
' }\n' +
' isDeprecated\n' +
' deprecationReason\n' +
' }\n' +
' inputFields {\n' +
' ...InputValue\n' +
' }\n' +
' interfaces {\n' +
' ...TypeRef\n' +
' }\n' +
' enumValues(includeDeprecated: true) {\n' +
' name\n' +
' description\n' +
' isDeprecated\n' +
' deprecationReason\n' +
' }\n' +
' possibleTypes {\n' +
' ...TypeRef\n' +
' }\n' +
' }\n' +
'\n' +
' fragment InputValue on InputValue {\n' +
' name\n' +
' description\n' +
' type { ...TypeRef }\n' +
' defaultValue\n' +
' \n' +
' \n' +
' }\n' +
'\n' +
' fragment TypeRef on Type {\n' +
' kind\n' +
' name\n' +
' ofType {\n' +
' kind\n' +
' name\n' +
' ofType {\n' +
' kind\n' +
' name\n' +
' ofType {\n' +
' kind\n' +
' name\n' +
' ofType {\n' +
' kind\n' +
' name\n' +
' ofType {\n' +
' kind\n' +
' name\n' +
' ofType {\n' +
' kind\n' +
' name\n' +
' ofType {\n' +
' kind\n' +
' name\n' +
' }\n' +
' }\n' +
' }\n' +
' }\n' +
' }\n' +
' }\n' +
' }\n' +
' }\n' +
' '
}
{ name: 'Tenet', director: 'kon' }
ㄴ 데이터를 가져오는 걸 확인할 수 있다.
—--
Patch 부
수정부
@Patch('/:id')
update(@Param('id') movieId: string, @Body() updateData) {
// return `this will update a movie with the id: ${movieId}`;
return {
updatedMovie: movieId,
...updateData,
};
}
}
http://localhost:3000/movies/12
post맨으로 날릴 데이터
{
"name": "Tenet",
"director": "kon"
}
리턴값
{
"updatedMovie": "12",
"name": "Tenet",
"director": "kon"
}
수정후 json을 돌려받았다
@Body() updateData
직접적으로 필요한 데이터는 요청해서 받아야한다
@Get("search")
search() {
return `We are searching for a movie with a title:`;
}
search를 통해 리퀘스트를 받도록 만들고 싶다
@Get("search")
search() {
return `We are searching for a movie with a title:`;
}
@Get("/:id")
getOne(@Param('id') id: string) {
return `this will return one movie with the id: ${id}`;
}
⭐️ Search 부분이 get 보다 밑에 있으면 NestJs는 search를 id로 판단
위치 이동 시킨 것
http://localhost:3000/movies/search?year=2000
@Get('search') //search
search(@Query("year") searchingYear:string) { //search?year=입력값
return `We are searching for a movie made after:${searchingYear}`;
}
뭔가를 요청하면 받아올 수 있다
Query parameters 랑 body decorators를 배웠다