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

what does http and https mean? and also i'm confusing this piece of code below

var https = require("https"); var username = "rostyklukavyi2"; function searchUserPoints (username, points, badges) { const message = My username is ${username} i have ${points} and i have ${badges} badge\'s; console.log(message); }

var request = https.get(https://teamtreehouse.com/${username}.json, response => {

                    var body = "";  <--- ?? why do we need to make this string!

                   response.on('data', data => {   <--???
                      body += data.toString();     <--??
                    });                            <--
                    response.on('end', () =>{
                      const profile = JSON.parse(body);   <--
                      searchUserPoints(username, profile.points.JavaScript, profile.badges.length);
                    });
                  });

1 Answer

Steven Parker
Steven Parker
229,952 Points

HTTP stands for "Hyper Text Transfer Protocol".

It's how web data moves on the internet. There's a sercurity version of it (with the letter "s" added) that encrypts the data to prevent interception by unauthorized parties along the transfer path.

And the reason to make body a string is that as data arrives, the callback functions concatenates an additional string to it ("body += data.toString()"). You can't add to a variable like this unless it has already been initialized.