@Get() 라우팅 엔드 포인트 트러블슈팅

hhyeong_0·2023년 5월 29일
0

@Get(), @Post() 등 활용시 라우팅 엔드 포인트 지정 주의할 점!!!

@Get('/a/:b'), @Post('/a/:b'), @Post('/a/:c') 와 같이 해당 URL로 들어오는 요청을 처리하기위한 라우팅 엔드 포인트에서 /:은 요청을 통해 넘겨주는 변수 부분이므로 위에 쓴 두 개의 Post는 결국엔 /a로 들어오는 하나의 요청에 대해 둘 다 작동되어 버린다...

아래는 내가 짰던 문제의 코드.....

@ApiOperation({
    summary: '게시글 생성',
    description:
      'post_type에 해당하는 게시판에 PostRequestDto에서 받아온 정보를 바탕으로 새로운 게시글을 생성합니다.',
  })
  @Post('/posts/:post_type')
  @UseGuards(AuthGuard('jwt')) 
  async createPost(
    @Body() postRequestDto: PostRequestDto,
    @Param('post_type') boardType: BoardType,
    @GetUser() user: UserDto,
  ) {
    postRequestDto.boardType = boardType;
    postRequestDto.userId = user.userId;
    const result = await this.boardService.createPost(postRequestDto);
    return result.postId;
  }

  @ApiOperation({
    summary: '게시글 세부내용 조회',
    description: 'post_id에 해당하는 게시글의 세부사항을 조회합니다.',
  })
  @Post('/posts/:post_id') // 이 부분이랑
  @UseGuards(AuthGuard('jwt'))
  async postShow(@Param('post_id') postId: number) {
    return await this.boardService.postShow(postId);
  }

코드를 살펴보면 알 수 있듯이 게시글 생성은 @Post('/posts/:post_type'), 게시글 조회도 @Post('/posts/:post_id')이므로 만약 "/posts/:17"로 Post 요청을 보내면 결국 게시글 생성, 생성 모두 같은 라우팅 엔드 포인트를 가지고 Post 요청을 처리하기 때문에 둘다 작동이 되어버려 문제가 발생한다.

진짜진짜진짜 거의 3시간동안 findOne, findOneBy 메소드를 잘못쓴건지 알고 reference도 없어서 공식문서 뒤지고 그랬는데.... ㅠㅠㅠ.

라우팅 엔드 포인트에 대한 정확한 공부없이 그냥 저렇게 쓰면 요청을 처리하는구나하고 개념 공부없이 대충 쓰면서 익숙해지면 이런 간단한 문제때문에 이렇게나 고생하는구나를 느낄 수 있었던 트러블슈팅이였다...🥲

profile
뭐라도 해보자 !

0개의 댓글