JavaScript Node.js Basics Building a Command Line Application Capturing Command Line Arguments

Cengiz Demir
Cengiz Demir
5,055 Points

Nodejs help

// 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

//Print Message funtion to console function printMessage(username, badgeCount, points){ const message = ${username } has ${badgeCount} total badges and ${points} points in Javascript; console.log(message); }

//printMessage("cengizdemir", 100, 2000000);

// Conntect to API URL(; //Go to nodejs Documentation to https request GetMethod https.get

//Require HTTPS Modules const https = require('https'); //const username = "cengizdemir";

function getProfile(username){

const request = https.get(${username}.json, response =>{

                 //console.dir(response);//method,domain and a lof of variables

                 let body = "";
                 // Read the data
                response.on('data', data =>{
                  //console.log('data:', data);// data comes like a number
                  //console.log(typeof data); //object

                   //console.log('data:', data.toString()); // datas comes like a json 
                   //console.log(typeof data); //object

                   body += data.toString(); 
                response.on('end', ()=>{
                  //console.log(typeof body);//string
                    // Parse the date
                  const profile = JSON.parse(body);

                    //console.dir(profile); Like a orjinal format
                    //console.log(typeof profile);//object
                  // Print the data
                  printMessage(username, profile.badges.length, profile.points.JavaScript);



} //getProfile("cengizdemir"); //getProfile("chalkers"); //THIS IS AWESOME because chalkers comes before me the resion the https request response cycle finishes before mine, so it prints it out.

//forEach iterates over the array and passes each members into a callback function. const users = ["cengiz", "chalkers", "davemcfarland"];


//users.forEach(username => { // getProfile(username); //});

users.forEach(getProfile); doesn't work on my workspaces

1 Answer