Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript Node.js Basics 2017 Building a Command Line Application Getting the Response Body

data

Can someone please explain line 17.Thanks

Jiten Mehta
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Jiten Mehta
Front End Web Development Techdegree Graduate 21,209 Points

Hey Azzie, I was confused by this also. After a few google searches, i think I understand.

I have written out something similar to what Andrew has along with comments. This should hopefully make you understand better.

// Require https module
const https = require('https');

//Connect to API URL
const request = https.get('https://teamtreehouse.com/jitenmehta3.json', res =>{
    // this variable will contain data sent from the API URL.
    let body = '';

    // the on function registers an event handler for a specific event.
    // the first argument is the name of the event, data. 
    // the second argument is the callback function. A parameter called data is used.
    // the body variable will concatenate each piece of data(parameter) sent by the API and store it into the body variable.
    res.on('data', data =>{
        body += data.toString()
    });

    // The name of this event handler is end.
    // the second argument is a anonymous callback function which logs the body variable to the console.
    res.on('end', () =>{
        console.log(body);
    })
});

If this hasn't helped, try these links = https://www.w3schools.com/nodejs/nodejs_events.asp, https://stackoverflow.com/questions/17379050/node-js-on-function-what-does-it-do

2 Answers

Jiten Mehta
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Jiten Mehta
Front End Web Development Techdegree Graduate 21,209 Points

No worries Azzie Fuzzie . If you could give my response a thumbs up that would be great.

Also I found all the events related to stream events in Node. 'data' and 'end' are one of many.

Have a look = https://nodejs.org/api/stream.html#stream_stream

Thanks mate Jiten Mehta.Much appreciated!