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

I've followed this video line for line and, if run in the workspace, it returns a statusCode of 301 and stops.

I've followed this video line for line and, if run in the workspace, it returns a statusCode of 301 and stops. I've tried turning it into a real file and running it as well and I get a 301 code again. You simply cannot make the workspace do this.

2 Answers

The video is outdated. The site now uses https. Replace all instances of "http" with "https" in your code and you should get the desired result.

Scott Campbell
Scott Campbell
1,207 Points

Rafid is correct. Here is the updated code.

var https = require("https");
var username = "chalkers";

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



//Connect to the API URL
var request = https.get("https://teamtreehouse.com/" + username + ".json", function(response) {
  console.log(response.statusCode);

  //Read the Data
  var body = "";

  response.setEncoding('utf8');

  response.on('data', (chunk) => {
    body += chunk;
  });

  response.on('end', function() {
    console.log(body);
  });

  //Parse the Data
  //Print the Data
});

request.on("error", function(error){
  console.error(error.message);
});
Matt Whittle
Matt Whittle
8,280 Points

I don't believe ES6 works in this workspace yet.