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 Building a Command Line Application Getting the Response Body

karan Badhwar
seal-mask
.a{fill-rule:evenodd;}techdegree
karan Badhwar
Web Development Techdegree Student 15,257 Points

Unable to understand

I am not able to understand how the data is moving....

How the data is moving on the 'DATA' Event. What are we getting back from the request in the first case..

Secondly, I am not getting it how data is getting carry forward from the callback to events. will we be going more in depth ahead?

Thirdly, how the response got passed on the data and I do not find any explanation about that too?

   const request = https.get('https://teamtreehouse.com/karankaran.json', (res) => {
     let body ='';
    res.on('data', response => {
     body += response;
      console.dir(response); THIS ONE RETURNS BUFFERS
     console.dir(body); WHILE THIS ONE RETURNS A FILE CONVERTED INTO STRING
    });
Did I miss any course behind coz, I don't find it for beginners ? it is explained as we know some part of it before

1 Answer

Caleb Kemp
Caleb Kemp
12,677 Points

Happy to help :smile:

  • how is the data moving

It starts with a get request to the endpoint URL. Since the amount of data returned could potentially be very large, it is sent as "chunks" of binary ( 1's and 0's) data. Your code your continue requesting data until the server returns a value of null. At which point your code will know that it has all the data, and will trigger the "end" event. By default, the data is returned as a Buffer object unless an encoding has been specified using the readable.setEncoding() method or the stream is operating in object mode.

data flow recap, you request data, the server returns binary data which gets returned as a buffer object.


Secondly, I am not getting it how data is getting carry forward from the callback to events.

The events are kind of like status codes, if status code = 1 do x, if 2 something else. Think of it this way, you are getting a package delivered and they give you status updates. code 1, "received by the post office", code 2, "arrived at your city", code 3, "at your door." So, after the post office receives your package, they send you a text with the code of 1. Maybe you wanted to call the post office to have the box marked fragile. So, you write a reminder,if code = 1, call the post office.. When it gets to your city you want a reminder to make space for the package. if code = 2, clean garage. And when you get the final code (3), you want to grab the package if code = 3, grab package from door. As you can see, it's not so much that the data is getting stored in different event variables and that data is saved for you to work on. It's more like, while the data is being processed, you can interrupt the process and have code run. Whatever data it had when you interrupted it, is the data it will have. I hope that makes sense


Last part, if I'm getting a buffer object, why does

console.dir(response); THIS ONE RETURNS BUFFERS

console.dir(body); WHILE THIS ONE RETURNS A FILE CONVERTED INTO STRING

Since a buffer object is returned, printing the response variable unsurprisingly prints the buffer object. The part that probably seemed much more confusing was why the body variable which is just body += response, was returned as text.

But looking at an excerpt from the node.js documentation here helps provide the answer

The listener callback will be passed the chunk of data as a string if a default encoding has been specified

So, when you saved the response object as a variable, encoding was applied. Since no encoding was specified, the default encoding was used which passes the data in the string format, or more specifically as utf-8.

Happy to help, I hope that helps, let me know if it gives you any more trouble :grinning:

karan Badhwar
seal-mask
.a{fill-rule:evenodd;}techdegree
karan Badhwar
Web Development Techdegree Student 15,257 Points

Thankyou Caleb Kemp, for the explanation , I got some of the part, but I am still confused I guess like in the Third part about encoding, as I never heard anything about these terms before and all of a sudden in the videos, Reggie just showed what to do how to do and nothing is explained, I had to watch a node.js basics crash course on Youtube to understand about Modules and all. Do you have any Idea, that is this just an overview course, because the way everything was done it was more of sort of just Remember everything..

Caleb Kemp
Caleb Kemp
12,677 Points

You probably know, that your computer only handles values of "on' or "off" (1's or 0's). So that when your computer saves text on your computer, it can't save say "word" as "word", it has to save it as a sequence of numbers. So, when your computer saves "word" into a string, it is automatically going to encode it for storage. And when you print it to the console, it will translate it back into the format it thinks you want (text). However, when you printed the response not saved as a string, it prints the raw data.

Basics of encoding. One of the web's first encoding protocols (ASCII) the letter "A", was represented by the number "065", which the computer would store in binary as "01000001". ASCII was limited to 256 characters, which was not enough characters to represent all the languages, special characters, and emojis being used. So, people started creating their own versions of ASCII to handle their particular needs. This led to the creation of lots of different encoding formats, which caused problems. If you sent a message to someone using a different ASCII encoding, it might not translate the message into the characters you intended, often leaving an unreadable message. A lot of this was rectified with the utf-8 encoding format which now handles 95% of the content of the web (and is the node encoding default). However, if that is not the encoding you wanted, Node also supports some other encoding formats as well (such as hex) which will be applied if specified. Here is the doc that lists all the Node supported formats and their syntax. I put a few examples below of what the syntax of encoding looks like.

So from earlier

console.dir(response); THIS ONE RETURNS BUFFERS

console.dir(body); WHILE THIS ONE RETURNS A FILE CONVERTED INTO STRING

say if you wanted both to print console.dir(response) as utf8 text you would write

console.dir(response).toString('utf8');

if you wanted to print console.dir(body); seeing the actual utf8 encoding values (like the unformatted buffer response gave you) you could write

console.dir(Buffer.from(body, 'utf8'));

and here is how you would print both in hex code

console.dir(response).toString('hex');
console.dir(Buffer.from(body, 'utf8').toString('hex'));

Here is a link about the basics of encoding if you wanted to explore the topic further. I hope that helps :smile:

Caleb Kemp
Caleb Kemp
12,677 Points

Happy I was able to help :relaxed: