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 Node.js Basics 2017 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(https://teamtreehouse.com/cengizdemir.json); //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(https://teamtreehouse.com/${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