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 Building a Command Line Application Requesting data with https

karan Badhwar
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
karan Badhwar
Web Development Techdegree Graduate 18,135 Points

require, API (data) and more

Couple of questions...

1 - I did not understand what does require() method do?

2- As he kept on saying response method, so the data received from the API, was that a Promise Object?

3 - How console.dir() even displayed the status Message (which console.log() does not show), generally it just prints the data receive so what are we getting in return as data?

karan Badhwar
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
karan Badhwar
Web Development Techdegree Graduate 18,135 Points

Daoud Merchant, thankyou sir, it's clear now. I get it now so by the way about response object, I am not clear so are we passing an object to the callback ?

No problem!

We are not passing an object in to a callback, but the function http.get() is. It performs the request for the resource at the string URI of the first argument, then when it receives that response object passes it to the callback we provide.

Here's a basic example of the same API:

const doubleAndPassOn = (num, cb) => {
  const doubledNum = num * 2; // Do something with first argument
  return cb(doubledNum);  // Pass what you did with the first argument to the function provided as the second argument
};

You would call it like http.get(), with two arguments (a value and a function):

doubleAndPassOn(2, doubledNum => {
  console.log(`The number doubled is ${doubledNum}.`);
}); // "The number doubled is 4."

Does that make more sense?

1 Answer

1) require accesses a module so that it can be used within the current file. By assigning its return value to a variable, the module's methods and properties can be accessed on it (https.get() etc.). Node has many built-in modules (including https), but they still need requiring in each file.

2) .get() is a method of the https module. It does not return a promise, hence requiring a callback parameter to deal with the response once it has been received (for asynchronicity). Libraries (such as Axios) help with promise-based server code.

3) Depending on your environment/client, the difference between console.log and console.dir may be very subtle (Chrome dev tools gives both disclosure triangles), but .statusCode is a property of the 'response object' (there are various different kinds of response object, best not to go in to that so soon), which is what .get() passes to the callback once the response is received. Mr. Williams initially logs this entire object, hence the enormous block of code containing all key value pairs (including statusCode: 200 buried in somewhere if you comb through it line by line). Later, only accessing the response object's .statusCode property, only the value of '200' is logged.

Replace 'csalgado' with some nonsense for a non-existing user, save and run the file in the terminal again. The console will log '404' (the statusCode for 'not found'), meaning that the server successfully responded but the data requested does not exist.

Hope that helps, let me know if anything's still not clear.