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

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 (https://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,810 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,606 Points

Thanks for linking to this thread

Awesome! Thanks!

Roy Penrod
Roy Penrod
19,810 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,810 Points

Glad it helped, Samantha.