Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

JavaScript Node.js Basics (2014) Building a Command Line Application Getting the Response Body

Mariusz Dabrowski
Mariusz Dabrowski
6,132 Points

Response chunk not what is expected

Here is my code:

const https = require('https');

var request = https.get('https://teamtreehouse.com/mariuszdabrowski.json', function(response) {
  response.on('data', function(chunk){
    console.log(chunk);
  });
});

request.on('error', function(error){
  console.error(error.message);
});

Here is the output in Terminal:

<Buffer 7b 22 6e 61 6d 65 22 3a 22 4d 61 72 69 75 73 7a 20 44 61 62 72 6f 77 73 6b 69 22 2c 22 70 72 6f 66 69 6c 65 5f 6e 61 6d 65 22 3a 22 6d 61 72 69 75 73 ... >
<Buffer 65 65 68 6f 75 73 65 2e 63 6f 6d 2f 62 61 64 67 65 73 5f 4a 61 76 61 53 63 72 69 70 74 5f 41 4a 41 58 42 61 73 69 63 73 5f 53 74 61 67 65 33 2e 70 6e ... >
<Buffer 73 65 2e 63 6f 6d 2f 6c 69 62 72 61 72 79 2f 6a 61 76 61 73 63 72 69 70 74 2d 62 61 73 69 63 73 2f 73 74 6f 72 69 6e 67 2d 61 6e 64 2d 74 72 61 63 6b ... >
<Buffer 64 22 3a 36 32 37 32 2c 22 6e 61 6d 65 22 3a 22 43 6f 6d 70 69 6c 65 20 53 61 73 73 20 77 69 74 68 20 47 75 6c 70 22 2c 22 75 72 6c 22 3a 22 68 74 74 ... >
<Buffer 61 72 79 2f 6e 6f 64 65 6a 73 2d 62 61 73 69 63 73 2f 69 6e 74 72 6f 64 75 63 74 69 6f 6e 2d 74 6f 2d 6e 6f 64 65 6a 73 22 2c 22 62 61 64 67 65 5f 63 ... >

Does anybody know why my output is not the JSON that you see in the video?

UPDATE

Just incase anybody else runs into this issue it has to do with encoding. Here's where I found the solution: http://stackoverflow.com/a/11709054/2040509

If anybody wants to jump in and provide some more feedback or explain why this is happening that would be awesome.

chaz
chaz
25,372 Points

having a similar issue— BUMP^

Also hit this problem - thanks for providing this solution!

1 Answer

Nicolas Hampton
Nicolas Hampton
44,626 Points

Mariusz,

Andrew is using a clever trick to make that buffer readable. It's called type conversion. You've probably come across it when adding a number to a string, like if you were to run the node repl and do this:

> "something" + 1
'something1'

The same thing is happening when Andrew logs "body:" + chunk. The chunk is actually a buffer type that's pretty unreadable but convenient to send data. But the buffer is implicitly converted to a string when added to a string, just like an integer is. I hope that explains a little of that for you, and happy coding!