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'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.