[Restful] REST - 주석 DELETE

Zero·2023년 3월 13일
0

REST

목록 보기
8/8

DELETE 기능 구현하기

app.delete('/comments/:id', (req,res) => {
  const {id} = req.params;
  comments = comments.filter(c => c.id !== id)
  res.redirect('/comments');
})
<!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>
  <a href="/comments">Back to index</a>
  <br />
  <a href="/comments/<%= comment.id %>/edit">go To comment Modify</a>
  <form method="post" action="/comments/<%= comment.id %>?_method=DELETE">
    <button>DELETE</button>
  </form>
</body>

</html>

--> woof woof woof !! - onlysayswoof 를 삭제한 모습

마찬가지로 delete 라우터를 생성하고, show.ejs 템플릿에 삭제 버튼을 하나 추가해준다.
delete 라우터에서는 filter 메소드를 통해서 id가 일치하지 않는 즉 , 삭제하고자 하는 comment를 제외한 나머지를 새로운 배열로 생성하고 , 해당 내용을 출력해줌으로써 최종적으로 삭제된 모습을 보여준다 !

0개의 댓글