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

why to store https.get () inside a request const in node js?

const https = require (https); const username = chalkers;

//* here is the problem const request = https.get(https://teamtreehouse.com/${username}.json, response => { console.log(response.statusCode); });

3 Answers

Steven Parker
Steven Parker
229,644 Points

The ClientRequest object represents the request created by the "https.get" and is used to establish handlers to determine the status of the request and and to receive incoming data.

For more information, see the documentation page.

Steven Parker
Steven Parker
229,644 Points

It sounds like you may be confused with how a "const" object can reflect any changes. But "const" doesn't mean the object itself cannot change, only that the variable cannot be changed to refer to a different object.

Steven Parker
Steven Parker
229,644 Points

The callback in the "get" is often all that is needed, but you can use the stored request if required. For example, you could use it to create an error handler:

// this uses the "request" previously stored:
request.on("error", error => {
    console.error(error.message);
});

look at this : in simple js const name = "barak"; , it dosent do anything , only when i write name in the console it return the value barak, here in node js when i store the request in a const isnt it to be the same thing???

Steve my problem is in clear understanding in the syntax , once we store a chanck of code inside of a variable, as shown here , We need to use it , it doesn’t helped with jest storing it

Steven Parker
Steven Parker
229,644 Points

See the additional comment I added to my answer.