BLOWING CHUNKS IN NODE.JS

Today I pair programmed a sprint that required using node.js to build a server that could handle requests from the client and send responses back to the client.

The two test that consistently hindered progress involved having sending responses that still contained data from previous posts. We were successfully able to push data from previous posts into our results array within our response object, but did not realize until much later that our response function was pushing objects that were still strings into our results array.

The reason our objects were still strings was because we failed to recognize that the chunk parameter taken by our callback in the response.on method was a typeof STRING. We fixed the bug by simply parsing our chunk parameter and pushing that parsed chunk into our results array.

response.results.push(chunk);
console.log(response.results)
//BUG
['{}', '{}'];
//CORRECT
response.results.push(JSON.parse(chunk));
console.log(response.results); // [{}, {}]

The moral of the story is to always console log the typeof of the data your are adding to your response object to ensure your server’s response is sent to the client correctly.