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 trialMadeline Yao
Full Stack JavaScript Techdegree Student 9,611 PointsWhy 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
231,269 PointsThe "https" module contains the code for the "https.get" method that the code is using to perform the request.
Routine Poutine
26,050 PointsIs a module another word for snippet?
Steven Parker
231,269 PointsA "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.
Routine Poutine
26,050 PointsSteven, Thatβs very helpful, thanks!
JASON LEE
17,352 PointsSo then wouldn't a "library" be the same as a "module"? Or is a library essentially just a really big module?
Steven Parker
231,269 PointsSure, a module could be described as a "library".