Web/node.js

nodeJS에서 html 대신 JSON response 보내는 법

SweetDev 2019. 10. 21. 16:53

HTML문서로 response를 보낼 때는 

res.writeHead('200', {'Content-Type': 'text/html; charset=utf8'});

res.write('<h2>사용자 리스트 조회 중 오류 발생 </h2>');

res.write('<p>' + err.stack + '/<p>');

res.end();

 

 

이런식으로 해서 보냈었다. 

 

그럼 JSON으로 해서 보내고 싶으면 어떻게 해야할 까?!?!?

 

res.writeHead('200', {'Content-Type': 'application/json; charset=utf8'});

res.write(JSON.stringify(results));

res.end();

 

이런식으로 보내면 된다고 한다!