Notice
Recent Posts
Recent Comments
Link
«   2026/02   »
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
Archives
Today
Total
관리 메뉴

사고쳤어요

[Node.js] 유튜브 서비스 - 채널 삭제 서비스(DB 연결) 본문

웹 풀스택

[Node.js] 유튜브 서비스 - 채널 삭제 서비스(DB 연결)

kevinmj12 2025. 2. 27. 13:35

https://makeaccident.tistory.com/124

 

[Node.js] 유튜브 서비스 - 채널 삭제, 채널 수정(DELETE, PUT)

채널 수정.put((req, res) => { let { id } = req.params; id = parseInt(id); let newChannelTitle = req.body.channelTitle; let channel = channelDb.get(id); let oldChannelTitle = channel.channelTitle; if (channel) { channel.channelTitle = newChannelTitle; c

makeaccident.tistory.com

채널 삭제 서비스를 데이터베이스에 연결하여 구현해보자.

 

.delete(param("id").notEmpty(), (req, res) => {
    validationError(req, res);

    let { id } = req.params;
    id = parseInt(id);

    let sql = `DELETE FROM channels WHERE id=?`;
    conn.query(sql, id, (err, rows, fields) => {
      if (err) {
        return res.status(400).json({
          message: `err: ${err}`,
        });
      }
      if (rows.affectedRows) {
        res.json({
          message: "채널 삭제가 완료되었습니다.",
        });
      } else {
        res.status(404).json({
          message: "채널 정보를 찾을 수 없습니다.",
        });
      }
    });
  });

이전 채널 수정을 할 때와 마찬가지로, affectedRows를 통해 예외 처리를 잘 진행해준다면 어렵지 않게 구현할 수 있다.