🚫ERROR
[Node.js] "UnhandledPromiseRejectionWarning: Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client" 오류
Cocoon_
2021. 10. 29. 01:25
반응형
🚀
Node.js로 개발 시 한번씩 겪어봤을 에러입니다.
"UnhandledPromiseRejectionWarning: Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client"
[ERR_HTTP_HEADERS_SENT]오류는 서버가 클라이언트에게 2개 이상의 응답을 보내려고 할 때 발생하는 오류입니다. 저같은 경우에는 if문으로 응답을 작성하고 다음 else문으로 해서 응답을 작성해야하는데 오류가 발생해서 찾아보니 else문을 작성하지 않아 위와같은 오류를 발생시키게 되었습니다.
<수정 전>
app.post('/login', async (req, res) => {
/* 변수 선언 등등*/
if{
/* 변수 처리문 */
return res.status(201).json({
/* 응답 메시지 */
});
}
return res.status(201).json({
/* 응답 메시지 */
});
});
<수정 후>
app.post('/login', async (req, res) => {
/* 변수 선언 등등*/
if{
/* 변수 처리문 */
return res.status(201).json({
/* 응답 메시지 */
});
}else{
return res.status(201).json({
/* 응답 메시지 */
});
}
});
반응형