본문 바로가기
웹 백엔드(Web BackEnd)

[웹 백엔드] 클라이언트로부터 온 중복된 데이터 방지

by 안한량 2023. 5. 16.
728x90
let processedData = [];
 
app.post("/calc", (req, res) => {
  // 클라이언트로부터 받은 데이터
  const clientData = req.body;
 
  // 동일한 데이터가 이미 처리되었는지 확인
  const isDuplicate = processedData.some((data) => {
    return JSON.stringify(data) === JSON.stringify(clientData);
  });

  if (!isDuplicate) {
    // 중복된 데이터가 아닌 경우에만 실행
    const clientDataWithDate = {
        ...clientData,
        date : new Date().toISOString().slice(0, -5)
    };
    console.log(JSON.stringify(clientDataWithDate, null, 2));
  }

  // 데이터 저장 및 응답
  processedData.push(clientData);
  res.status(204).send();
});
 
 
 

비교를 위한 processedData 변수 선언,

 

post 형식으로 클라이언트로부터 데이터를 받아 저장 후 뒤에 날짜를 추가해 줬는데

 

여기서 핵심은 날짜를 서버로직에서 추가해 주는 것이다,

 

날짜를 프런트엔드에서 넣어서 보내게 되면 초단위로 데이터값이 변경되어 중복데이터가 아니게 되어버린다.

728x90