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 receiving a 301 error.

I can seem to find the error in my code. Already made sure the url is correct since that is usually why I get this error.

profile.js

//Problem: Need a simple way to look at users badge count and JS points
//Solution: Use node.js to connect to TH API to get profile info to print out

var http = require("http");


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

//print out error messages
function printError(error){
  console.error(error.message);
};

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

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

    response.on('end', function(){
      if(response.statusCode === 200){
        try {
        //Parse the data
        var profile =  JSON.parse(body);
        //Print the data
        printMessage(username, profile.badges.length, profile.points.JavaScript);
        } catch(error) {
            //parse error here
            printError(error);
          }
        } else {
           //Status code error
           printError({message: "There was an error getting the profile for " + username + ". (" + http.STATUS_CODES[response.statusCode] + ")" });
          }
     });
  });
  //Connection Error
  request.on("error", printError);
}


module.exports.get = get;

app.js

var profile = require("./profile.js");


profile.get("johnrenzema");

Thank you for the help.

2 Answers

Thanks, I found that response just after posting this.

This really should be show in the course. This program worked perfectly the other day. I changed nothing, and was just finishing up the course. Caused it all to fail, really frustrating.