[Restful] REST - 주석 Show

Zero·2023년 3월 13일
0

REST

목록 보기
5/8

Show route / detail route

이번 포스팅에선 흔히 Show rout , detail route라고도 부르는 걸 구현하는 방법에 대해서 알아보도록 하자.

그렇다면 Show route가 뭘까?

show route는 특정한 하나의 리소스에 대한 디테일을 주로 확장된 보기 형식으로 보여주는 것을 말한다. 해당 라우트는 일단 어떤 리소스에 대한 고유의 식별자가 있어야 작동을 하는데, 예시를 통해 살펴보자.



// 해당 comments에 임의의 id를 추가해준다.
const comments = [
  {
    id : 1,
    username : 'Todd',
    comment : 'lol that is so funny !'
  },
  {
    id : 2,
    username : 'Skyler',
    comment : 'I like to go birdwatching with my dog'
  },
  {
    id : 3,
    username : 'Sk8erBoi',
    comment : 'Plz delete your account, Todd'
  },
  {
    id : 4,
    username : 'onlysayswoof',
    comment : 'woof woof woof !!'
  }
]

// comments/id 를 입력하게 되면 해당 id에 맞는 comment의 내용과, username을 화면단에 나타나도록 하자.
app.get('/comments/:id', (req,res) => {
  const {id} = req.params;
  const comment = comments.find( c => c.id === parseInt(id))
  res.render('comments/show' , {comment});
})
//show.ejs

<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Show</title>
</head>

<body>
  <h1>Comment id: <%= comment.id %>
  </h1>
  <h2>
    <%= comment.comment %> - <%= comment.username %>
  </h2>
</body>

</html>

  • comments/2를 입력하면 id가 2인 comment 정보가 출력되는 것을 확인할 수 있다.

0개의 댓글