Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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

kushalmahajan2
kushalmahajan2
7,683 Points

Not able to get message printed.

Below is the error message received after running app.js. Can anyone please let me know what is the issue.

treehouse:~/workspace$ node app.js                                                                                                          
undefined:0                                                                                                                                 


SyntaxError: Unexpected end of input                                                                                                        
    at Object.parse (native)                                                                                                                
    at IncomingMessage.<anonymous> (/home/treehouse/workspace/app.js:21:24)                                                                 
    at IncomingMessage.emit (events.js:129:20)                                                                                              
    at _stream_readable.js:908:16                                                                                                           
    at process._tickCallback (node.js:355:11)

My workspace link : http://w.trhou.se/8fm7oamm8o

1 Answer

Hi,

If you go back to https://teamtreehouse.com/library/nodejs-basics/building-a-command-line-application/making-a-get-request-with-http and have a look at the teachers notes there is a message to say:

"On October 20th 2015, we upgraded our website to only use HTTPS rather than just HTTP. This mean more security for anyone using our site.

This does include some breaking changes to our code. But it's simple to fix.

Replace any references to http to https. For example:

var https = require("https");

Modify any links from "http://teamtreehouse.com/" to "https://teamtreehouse.com/"."

So I believe that the following code will now work:

//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
var https = require("https");
var username = "kushalmahajan2";

function printMessage(username, badgeCount, points) {
  var message = username + " has " + badgeCount + " total badge(s) and " + points + " points in JavaScript";
  console.log(message);
}

//Connect to the API URL (http://teamtreehouse.com/username.json)
var request = https.get("https://teamtreehouse.com/" + username + ".json", function(response){
  var body = "";
  //Read the data
  response.on('data', function (chunk) {
    body += chunk;
  });

  //Parse the data
  response.on('end', function(){
    var profile = JSON.parse(body);
    //Print the data
    printMessage(username, profile.badges.length, profile.points.JavaScript);
  });



});

request.on("error", function(error){
  console.error(error.message);
});

This change over to use https will also cause an issue when you get to the "Handling Parsing and Status Code Errors" video. When you get there have a look at the Questions section under that video for some solutions.

Jamie Tuffen
Jamie Tuffen
8,042 Points

Thank you for this. It helped a lot. After changing all the HTTP's to HTTPS it worked.