[ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client

sangwoo noh·2022년 3월 23일
0

nodejs

목록 보기
1/1

또 뭔데?

[ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client

서버가 클라이언트로 복수의 응답을 보내려고 할때 발생하는 에러라고한다.
ㅇㅋ...

뭐 예를들면 이런경우

  • res를 두번 실행하게된답니다.
if(condition){
   res.status(400).json({
      ok:false,
      error:"some fucking error"
   })
}

res.status.(200).json({
   ok:true
})

조건문에서 return을 넣어 진행을 막아주면 됩니다.

  • 암튼 중요한건 응답이 복수로 전달되는것이 아니라 단 한개만 전달되게 로직을 짜면 됩니다요.
if(condition){
   return res.status(400).json({
      ok:false,
      error:"error that some fucking reason"
   })
}

return res.status.(200).json({
   ok:true
})

return을 의미론적으로라도 잘 사용하는 습관을 들여야지..

profile
하기로 했으면 하자

0개의 댓글