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 Getting the Response Body

Is anyone else get an http response code 301?

//Problem: we need a simple way to look at a user's badge count and JS points
//Solutions: Use node.js to connect to Treehouse's API and pull in points and badges
var http = require("http");
var username = "chalkers";
function printMessage(username, badgeCount, points) {
  var message = username + " has " + badgeCount + " total badges(s) and " + points + " points in JavaScript.";
  console.log(message)
}
//Connect to the API url (http://teamtreehouse.com/<username>.json
var request = http.get("http://teamtreehouse.com/" + username + ".json", function(response){
  console.log(response.statusCode);

//Read the data
//parse the data
//print the data out

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

My code is above...seems to be identical to Andrew's in the video but now I'm getting a 301 response when I run node app.js

4 Answers

Adam Hansen
Adam Hansen
14,420 Points

Just for clarity (and the lazy people)... You simply need to change

var http = require("http"); 

and

var request = http.get("https://teamtreehouse.com/" + username +".json"

into

var https = require("https"); 

and

var request = https.get("https://teamtreehouse.com/" + username +".json"

Then you should be fine!

Roy Penrod
Roy Penrod
19,809 Points

Hey, Evan. Don't worry ... you didn't do anything wrong. It happened to all of us.

Treehouse recently changed all of their http to https for security purposes.

Andrew wrote a response explaining it here.

__ROLLER__ Angel
__ROLLER__ Angel
25,594 Points

Thanks for linking to this thread

Awesome! Thanks!

Roy Penrod
Roy Penrod
19,809 Points

You're welcome, Evan.

Oh, awesome! Thanks for posting this question! I had the same problem, looked it up, realized it was a redirection issue but totally missed the https thing. Awesome

Roy Penrod
Roy Penrod
19,809 Points

Glad it helped, Samantha.