Notice
Recent Posts
Recent Comments
Link
«   2025/05   »
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 29 30 31
Archives
Today
Total
관리 메뉴

사고쳤어요

[Node.js] 유튜브 서비스 - 채널 생성, 채널 조회(POST, GET) 본문

웹 풀스택

[Node.js] 유튜브 서비스 - 채널 생성, 채널 조회(POST, GET)

kevinmj12 2025. 2. 20. 17:16

채널 생성

const express = require("express");
const app = express();
app.listen(3000);
app.use(express.json());

let channelDb = new Map();
var id = 1;

app
  .route("/channels")
  // 채널 전체 조회
  .get((req, res) => {})
  // 채널 생성
  .post((req, res) => {
    let { channelTitle } = req.body;
    if (channelTitle) {
      channelDb.set(id++, req.body);
      res.status(201).json({
        message: `${channelTitle} 채널이 생성되었습니다.`,
      });
    } else {
      res.status(400).json({
        message: "요청이 올바르지 않습니다.",
      });
    }
  });

채널 조회

app
  .route("/channels/:id")
  // 채널 개별 조회
  .get((req, res) => {
    let { id } = req.params;
    id = parseInt(id);

    let channel = channelDb.get(id);
    if (channel) {
      res.json({
        channelTitle: channel.channelTitle,
      });
    } else {
      res.status(404).json({
        message: "요청이 올바르지 않습니다.",
      });
    }
  })