J.Y.S
¿ whats my interest ?
J.Y.S
전체 방문자
오늘
어제
  • 분류 전체보기 (59)
    • 트러블슈팅 기록 (7)
    • Java&Kotlin (5)
    • Server (22)
    • 데이터 엔지니어링 (3)
    • Architecture& Design Patter.. (1)
    • Daily (11)
    • 알고리즘 공부 (9)

블로그 메뉴

  • 홈
  • 태그

공지사항

인기 글

최근 댓글

최근 글

티스토리

hELLO · Designed By 정상우.
J.Y.S

¿ whats my interest ?

Server

[] ERR_HTTP_HEADERS_SENT : Client 요청에 두 번 이상 응답할 때 발생하는 에러

2021. 7. 18. 03:18

node.js 를 공부하면서 다음과 같은 에러가 발생하였다 :

Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client at ServerResponse.setHeader (_http_outgoing.js:558:11) at ServerResponse.header (C:\Users\popo3\Documents\Todo_List\node_modules\express\lib\response.js:767:10) at ServerResponse.json (C:\Users\popo3\Documents\Todo_List\node_modules\express\lib\response.js:264:10) at

C:\Users\popo3\Documents\Todo_List\routes\todo.js:21:17 at FSReqCallback.oncomplete (fs.js:171:23)

{ code: 'ERR_HTTP_HEADERS_SENT' }

 

해당 에러는 client 요청에 두 번 이상 응답(response) 하는 경우 발생하는 경우이다.

 

1. 문제 : res.json() 과 res.render() 두 함수 모두 res.end() 를 마지막에 호출하고 있기 때문에 한 함수에서 같이 사용하면 문제가 된다.

exports.list = function(req,res) {
    var exists = fs.existsSync('/todo_list.json');
    if (exists) {
        console.log('read from json file');
        fs.readFile('./todo_list.json', 
        {'encoding' : 'utf-8'}, 
        function(err, list) {
            res.json(list);
        });
    }
    else {
        console.log('create json file');
        var list = {
            'list': []
        }; 
        fs.writeFile('./todo_list.json', JSON.stringify(list), function(error) {
            res.json(list);
        });
    }
    res.render('todo.ejs', {'title' : 'to-do' });
};

2. 해결방안 :

저작자표시 (새창열림)

'Server' 카테고리의 다른 글

[node.js] 간단 HTTP 서버 띄우기 (웹 서버 호스팅하기)  (0) 2021.10.06
JSON: javascript 객체 표기법  (0) 2021.08.06
Express Generator : 간단한 웹페이지 만들기  (0) 2021.07.14
[Express] app.listen 과 http.createServer(app) 의 차이  (3) 2021.07.07
Express ExecutionPolicy 관련 오류  (0) 2021.07.06
    'Server' 카테고리의 다른 글
    • [node.js] 간단 HTTP 서버 띄우기 (웹 서버 호스팅하기)
    • JSON: javascript 객체 표기법
    • Express Generator : 간단한 웹페이지 만들기
    • [Express] app.listen 과 http.createServer(app) 의 차이
    J.Y.S
    J.Y.S

    티스토리툴바