JavaScript Node.js Basics Building a Command Line Application Making a GET Request with https

how come in node .js const request = is to set , and is js is a varible

i cant understand i come from java script backgorund and there we use const as a var to store data , here in node,js the is use to set?? the requast ? and were does it used and return the value ??

9 Answers

Steven Parker
Steven Parker
171,120 Points

As commonly used in many computer languages, the equal sign is the assignment operator, it copies the value of the expression on the right side into the storage location referenced on the left.

// so it stroes the the request in a const , but how it executed (triggerd)??. it jest stores it .

const request = https.get('https://encrypted.google.com/', (res) => { console.log('statusCode:', res.statusCode); console.log('headers:', res.headers);

res.on('data', (d) => { process.stdout.write(d); });

Steven Parker
Steven Parker
171,120 Points

This is an intermediate level course, but it seems like you're asking questions about the basic fundamentals of the language. If you're not already familiar with JavaScript, you might want to set this course aside and take something like Beginning JavaScript first.

remedies
remedies
3,614 Points

I think OP might be asking why we needed

const request

in

const request = https.get('https://teamtreehouse.com/remedies.json', response => {
  console.log(response.statusCode);
})

The return value of https.get() is stored in request. If you look at the API documentation https://nodejs.org/docs/latest-v9.x/api/https.html#https_https_request_options_callback, you will see that we are able to access properties of the request object such as the .on() method

request.on('error', (e) => {
  console.error(e);
});

You CAN remove const request and you will still be able to get and parse the JSON file, but you won't be able to access the methods and properties that would've been assigned to the request.

In simple English what does the equal = stands for in in node js ?

what i am asking is once you put the https get method inside the const , now what you need to do?? did you watch the vedio???

simple example is: // connect to the api url ()

const request = https,get... see it stores the method inside the const but how it connect to the api? it jest stores it .

Steven Parker
Steven Parker
171,120 Points

It does not store the method. It calls the method and stores the result, which is an http.ClientRequest object.

second example var http = require('http'); The http variable represents the JavaScript object that will let us launch a web server. in simple javascript that var need to be part of a call function to do somthing . like var name = "barak"; console log ( name); the console log method write it on the screen , without the this method the var is jest a var

i think i finally understand what i am asking , is why in that vedio we stored the https.get() when we conncted to Treehouse API inside a const ??? thanke you!

Routine Poutine
Routine Poutine
22,764 Points

These answers helped me a lot:

Steven Parker on Jun 24 As commonly used in many computer languages, the equal sign is the assignment operator, it copies the value of the >expression on the right side into the storage location referenced on the left.

Steven Parker on Jun 25 It does not store the method. It calls the method and stores the result, which is an http.ClientRequest object.

I know JavaScript, but was thinking let https was arbitrary, when in fact require('https') is not; by reading right to left it makes sense that the require('https') gets an object and the let https creates a variable to contain it.

It's crazy how I couldn't get this for two days; so simple, yet mind-boggling at first. I even asked Gitter for help in their NodeJS chatroom. No wonder they didn't see why I was confused. Thanks Steven!