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

Madeline Yao
Madeline Yao
Full Stack JavaScript Techdegree Student 9,557 Points

Why we need to require module in order to gain information from API?

// Problem: We need a simple way to look at a user's badge count and JavaScript points // Solution: Use Node.js to connect to Treehouse's API to get profile information to print out

//Require https module const https = require('https'); const username = "chalkers";

//Function to print message to console function printMessage(username, badgeCount, points) { const message = ${username} has ${badgeCount} total badge(s) and ${points} points in JavaScript; console.log(message); }

// Connect to the API URL (https://teamtreehouse.com/username.json) const request = https.get(https://teamtreehouse.com/${username}.json, response => {

                      console.log(response.statusCode);

                      // Read the data
                      // Parse the data
                      // Print the data

                      });

In the video tutorial, I notice that when you do not require https, you cannot get information from API. I wonder why that happens. Could anyone please explain to me about that and the importance of the module? Thank you!

3 Answers

Steven Parker
Steven Parker
160,247 Points

The "https" module contains the code for the "https.get" method that the code is using to perform the request.

Matthew Leroe
Matthew Leroe
9,440 Points

Is a module another word for snippet?

Steven Parker
Steven Parker
160,247 Points

A "module" would be less than a whole program, but it would typically be in it's own file, and contain one or more complete functions and /or object definitions.

A "snippet" could be any small piece of code, perhaps only part of a function.

Matthew Leroe
Matthew Leroe
9,440 Points

Steven, That’s very helpful, thanks!