NestJS의 구조

package.json 에 대하여..

"dependencies": {
  // 여기서부터...
    "@nestjs/common": "^9.0.0", 
    "@nestjs/core": "^9.0.0",
    "@nestjs/platform-express": "^9.0.0",
  // 여기까지는 nestjs의 코어 모듈
  
    "reflect-metadata": "^0.1.13",  // decorator 문법을 사용하기 위한 패키지

    "rimraf": "^3.0.2", // 리눅스처럼, rm, rf *파일지울때 명령어  윈도우에서 사용하기 위함.
    "rxjs": "^7.2.0" // 비동기, 이벤트 기반 프로그램을 돌리기 위함
  },

컨트롤러

요청을 처리하고 응답을 반환함

http req ==> <== response

의존성 주입

모듈이 여러개가 합쳐져서 하나의 모듈을 만들고 하나의 모듈은 하나의 기능을 구현하고… 이렇게함으로써 유지보수성, 가독성, 등등을 높일 수 있다..

end point


위와 같이 사용하면 endpoint가 cats가 되어
localhost:8000 으로 보내면 못받고
localhost:8000/cats 로 받아야 데이터를 받아올 수 있는 것이다.

```@Get('hello')``` 
도 마찬가지

*Request*
```ts
import { Controller, Get, Req } from '@nestjs/common';
import { Request } from 'express';

...
getHello(@Req() req: Request): string {
    console.log(req);
    return this.appService.getHello();
}

위와 같이 request 값을 받아올 수 있게 된다.

Body, Param

@Get('hello/:id/:name')
getHello(@Req() req: Request, @Body() Body, @Param() param): string {
    console.log(req);
    console.log(param);

    return 'hello world';
}

위와 같이 사용하여

동적 라우팅에 해당하는 param 정보를 가져올 수 있다.

req.body req.param

뭐 이런식으로 받을 수 있지만, 아예 분리해서 받을 수 있으니 이렇게 받을 것!

결과 : { id: '21', name: 'nav' }

getHello(
    @Req() req: Request,
    @Body() Body,
    @Param() param: { id: string; name: string },
  ): string {
    // console.log(req);
    console.log(param);

    return 'hello world';
    // return this.appService.getHello();
}

이런식으로 형식 지정도 가능하다는 점.

나중에 DTO 에서 속성을 저장해주고, 이상한 요청을 거를 수 있음. (나중에 공부하게 됨.)