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 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
11,314 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.