회원 + 채널 API 고도화

Kyulee·2026년 1월 29일

간단한 실습

목록 보기
3/3
post-thumbnail

전에 만들었던 회원 API에 각 회원마다 채널을 가질수 있도록 채널 API를 수정해보도록 하겠습니다.
추가로 app.js에서 서버를 구동하기 위해서 기존에 각각 움직이던 User, Channel api는 라우터 파일로 변경하였습니다.

👉🏻 확인하러가기

API 수정

채널 생성과 조회 request에 userId를 추가했습니다.

1. 채널 생성 : Post /channel

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

4. 회원의 채널 전체 조회 Get /channels

  • req : body (userId)
  • res : 채널 전체 데이터 list, json array

구현

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

// 4. 회원의 채널 전체 조회
router.get("/channels", (req, res) => {
  const { user_id } = req.body;
  let channels = [];

  if (userId) {
    db.forEach(function (value, key) {
      if (value.user_id == user_id) {
        channels.push(value);
      }
    });
  }
  res.status(200).json(channels);
});

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

0개의 댓글