<스프린트 해결 시 중요하게 생각해 볼 문제>
<채터박스 서버 부분 POST 요청 풀이>
console.log("Serving request type " + request.method + " for url " + request.url);
//POST 요청 시 모든 리퀘스트의 메소드와 url을 로깅
else if (request.method === 'POST' && request.url === '/messages') {
let body = [];
request.on('data', (chunk) => {
body.push(chunk);// body라는 빈배열에 쪼개진 chunk 값을 push해 넣어준다.
//(request로 받아온 data는 buffer형태(2진법같은 기계어)
//console.log(body)
}).on('end', () => {
body = Buffer.concat(body).toString();
//모든 Buffer 인스턴스를 list함께 연결 한 결과를 문자열 화.
//(버퍼 객체들의 내용을 하나로 합쳐 새로운 버퍼를 만들어 리턴)
//console.log(body)
data.results.push(JSON.parse(body));
//JSON.parse 로 객체화하여 저장공간의 results에 넣어준다.
//console.log(JSON.parse(body))
response.writeHead(201, headers);//상태 코드와 헤더를 작성
response.end(JSON.stringify(data));//응답 바디 전송, 최종적으로 저장공간에 쌓인 값을
//JSON.stringify 하여 전송.
//console.log(JSON.stringify(data))
});
}
HTTP 트랜잭션 해부라는 공식 가이드 문서를 통해 도움을 많이 받았다.
(https://nodejs.org/ko/docs/guides/anatomy-of-an-http-transaction/)