채널 API 설계 및 구현

Kyulee·2026년 1월 28일

간단한 실습

목록 보기
2/3
post-thumbnail

이번에는 채널 API 설계와 구현을 해볼 예정입니다

API 명세

채널

  • 채널 생성
  • 채널 수정
  • 채널 삭제
  • 채널 조회

1. 채널 생성 : Post /channel

  • req : body(channelTitle)
  • res : ${channelTitle}님 채널을 응원합니다.
  • http status
    • 200 -> 채널 생성 완료 시

2. 채널 수정 : Put channel/:id

  • req : URl (id), body {channelTitle}
  • res : ${channelTitle}로 변경 되었습니다.
  • http status
    • 200 -> 정상 수정 완료했을 시

3. 채널 개별 삭제 DELETE /channel/:id

  • req : URL (id)
  • res : 삭제 되었습니다.

4. 채널 전체 조회 Get /channels

  • req : x
  • res : 채널 전체 데이터 list, json array

5. 채널 개별 조회 Get /channels/:id

  • req : URL (id)
  • res : 채널 개별 데이터

구현

1. 채널 생성

app.post("/channel", (req, res) => {
  const { channelTitle } = req.body;
  if (req.body) {
    db.set(id++, { channelTitle });
    res.status(200).json({
      message: `${channelTitle}님 환영합니다!!`,
    });
  }
});

2. 채널 개별 수정

app.put("/channel/:id", (req, res) => {
  const targetId = parseInt(req.params.id);
  const title = db.get(targetId);

  if (title) {
    newTitle = req.body.channelTitle;
    db.set(targetId, { channelTitle: newTitle });
    res.status(200).json({
      message: `${newTitle}로 변경되었습니다.`,
    });
  }
});

3. 채널 개별 삭제

app.delete("/channel/:id", (req, res) => {
  const targetId = parseInt(req.params.id);
  const channel = db.get(targetId);

  if (channel) {
    db.delete(targetId);
    res.status(200).json({
      message: `${channel.channelTitle}님이 정상적으로 탈퇴되었습니다.`,
    });
  } else {
    res.status(404).json({
      message: "해당 채널을 찾을 수 없습니다.",
    });
  }
});

4. 채널 전체 조회

app.get("/channels", (req, res) => {
  let channels = [];

  db.forEach(function (value, key) {
    channels.push(value);
  });

  res.status(200).json(channels);
});

5. 채널 개별 조회

app.get("/channels/:id", (req, res) => {
  const targetId = parseInt(req.params.id);
  const channel = db.get(targetId);

  if (channel) {
    res.status(200).json({
      channelTitle: channel.channelTitle,
    });
  } else {
    res.status(404).json({
      message: "일치한 채널명이 없습니다.",
    });
  }
});

profile
안녕하세요 매일의 배움을 기록으로 자산화하는 개발자 이규현입니다 😊

0개의 댓글