JavaScript Node.js Basics Building a Command Line Application Parsing JSON

James Blazer
James Blazer
3,836 Points

getting (node:374) warning

I am getting multiple responses with EventEmitter memory leak.

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

// Connect to the API URL (https://teamtreehouse.com/username.json) // Read the data (the data will come in as a string so we will need to parse it) // Parse the data // Print the data we obtained from the API

// this code allows us to use the HTTPS module in our code-- we can name the variable anything we want but convention says name it what you are requiring for ease of // // understanding const https = require('https');

// save USERNAME as a variable so we can change it in the future const username = "chalkers";

// first build a function to set out site to where we are going towards function printMessage(username, badgeCount, points) { const message = ${username} has ${badgeCount} total badges and ${points} points in JavaScript; console.log(message); }

printMessage("chalkers", 100, 2000000);

// define the request and set the response using an arrow function const request = https.get(https://teamtreehouse.com/${username}.json, response => { // log out response using the DIR method
console.dir(response); console.log(response.statusCode); // read in the body of the response-- create a variable to concatenate the string response if desired -- this is the READ THE DATA part let body = ""; response.on('data', data => { body += data.toString();

// this is the end of data handler
response.on('end', () => {
            // PARSE the data
            const profile = JSON.parse(body);
            // console.log(body);
            // console.dir(profile);
            // this code accesses the javaScript points from the profile object using dot notation
               printMessage(username, profile.badges.length, profile.points.JavaScript);
            });

});

});

Adam Beer
Adam Beer
10,646 Points

Hi James! You can use the snapshot function in the workspace and provide the link to that. Or wrap your code with 3 backticks (```) on the line before and after. If you specify the language after the first set of backticks, that'll help us with syntax highlighting.