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 (2014) Building a Command Line Application Getting the Response Body

I still get a 301 error. Obviously folks have encountered this before. Does anyone have a workaround for this issue?

Here's my code: var request = http.get("http://teamtreehouse.com/" + username + ".json", function(response){ console.log(response.statusCode); //Read the data response.on('data', function (chunk){ console.log('BODY: ' + chunk);

}); //Parse the data //Print the data });

3 Answers

Kevin Korte
Kevin Korte
28,148 Points

Try it and check, but I think the URL now has to be https instead of just http like the video shows. I think that's the fix. But let me know, I haven't done that course in forever.

Hi Kevin,

For some reason it keeps spitting out Body: not found.

Here's the code I have:

//Problem: We need a simple way to look at a users badge count and javascript points
//Solution: Use Node.js to connect to Treehouse API
var http = require("https");
var username = "reyrey";

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(https://teamtreehouse.com/username.json)

var request = http.get("https://teamtreehouse.com/" + username + ".json", function(response){
  var body = " ";


  //Read the data
  response.on('data', function (chunk){ 
    body += chunk;
  });

  response.on('end', function(){
  console.log(body);
    console.log(typeof body);
  });
//Parse the data
//Print the data
});

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

Changing two lines of code:

  1. var https = require("https");

and

  1. var request = https.get("https://teamtreehouse.com/" +".... etc...

made it go from a 301 to a 200 for me.

Kevin Korte so I concluded it's due to the username. For some reason when I try to pull the data associated with my profile it returns a not found result but when I run "Chalkers" as the username then it spits out all the JSON data. I feel that the issues is a result of authentication failing. Thoughts?

Kevin Korte
Kevin Korte
28,148 Points

I agree, I think your user profile is private, I can't click on it. So that might be way it's not finding it. When I replace your username with mine, your code works.

You don't have to do this, but I bet the problem is in your account settings, the "allow everyone to see your profile" is probably unchecked. I bet checking that will fix this.

settings page

Kevin Korte that was it! Thanks again!

Kevin Korte
Kevin Korte
28,148 Points

Awesome, no problem.