[TIL] 24.01.23

sssujijjl·2024년 1월 23일

[TIL]

목록 보기
22/51

[환경 변수 설정하기]

  • .env 파일 생성
  • 숨기고 싶은 내용을 변수처럼 할당해주기
  • dotenv.config(); server.js 작성
  • npm install dotenv 설치해주어야한다.
  • 환경변수로 설정한 부분을 작성할 때는 process.env.이름 이렇게 작성해주어야한다.

[method]

  • GET : 서버에 데이터 출력 요청할 때 사용
router.get("/products", async (req, res) => {
  try {
    const products = await Product.find()
      .select("_id title author status createAt")
      .sort("-createAt")
      .exec();
    return res.status(200).json({ products });
  } catch (err) {
    return res
      .status(500)
      .json({ errorMessage: "상품 목록을 불러오던 중 오류가 발생했습니다." });
  }
});
  • POST : 서버에 데이터 입력 요청할 때 사용
router.post("/products", async (req, res) => {
  const { title, content, author, password } = req.body;
  const status = "FOR-SALE";
  const createAt = new Date();

  if (!title || !content || !author || !password) {
    return res
      .status(400)
      .json({ errorMessage: "데이터 형식이 올바르지 않습니다." });
  }

  const product = new Product({
    title,
    content,
    author,
    password,
    status,
    createAt,
  });

  try {
    await product.save();
    return res.json({ product });
  } catch (err) {
    return res
      .status(500)
      .json({ errorMessage: "상품 등록 중 오류가 발생했습니다." });
  }
});
  • PUT : 서버에 데이터 수정 요청할 때 사용
router.put("/products/:productId", async (req, res) => {
  const { productId } = req.params;
  const { title, content, password, status } = req.body;

  if (!title || !content || !status || !productId) {
    return res
      .status(400)
      .json({ errorMessage: "데이터 형식이 올바르지 않습니다." });
  }

  try {
    const currentProduct = await Product.findById(productId);

    if (!currentProduct) {
      return res
        .status(404)
        .json({ errorMessage: "상품 조회에 실패하였습니다." });
    }

    if (title && content && status) {
      currentProduct.title = title;
      currentProduct.content = content;
      currentProduct.status = status;
    }

    if (password !== currentProduct.password) {
      return res
        .status(403)
        .json({ errorMessage: "상품을 수정할 권한이 존재하지 않습니다." });
    }

    await currentProduct.save();

    return res
      .status(200)
      .json({ currentProduct, message: "상품 정보를 수정하였습니다." });
  } catch (err) {
    return res
      .status(500)
      .json({ errorMessage: "상품 정보 수정 중 오류가 발생했습니다." });
  }
});
  • DELETE : 서버에 데이터 삭제 요청할 때 사용
router.delete("/products/:productId", async (req, res) => {
  const { productId } = req.params;
  const { password } = req.body;

  if (!productId || !password) {
    return res
      .status(400)
      .json({ errorMessage: "데이터 형식이 올바르지 않습니다." });
  }

  try {
    const currentProduct = await Product.findById(productId).exec();
    if (!currentProduct) {
      return res.status(404).json({ message: "상품을 조회에 실패하였습니다." });
    }

    if (password !== currentProduct.password) {
      return res
        .status(403)
        .json({ errorMessage: "상품을 삭제할 권한이 존재하지 않습니다." });
    }

    await Product.deleteOne({ _id: productId }).exec();

    return res.status(200).json({ message: "상품을 삭제하였습니다." });
  } catch (err) {
    return res
      .status(500)
      .json({ errorMessage: "상품 삭제 중 오류가 발생했습니다." });
  }
});

0개의 댓글