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

Sean Henderson
Sean Henderson
15,413 Points

Not seeing any out from console.log(chunk)

I've added the response.on call as Andrew does in the video:

//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
  response.on("data", function(chunk) {
    console.log("BODY: " + chunk);
    // THIS BLOCK
  });
  //Parse the data
  //Print the data out  
});

When I run it, I still see the status code from the previous block but nothing with "BODY: ":

treehouse:~/workspace$ node app.js
301 
treehouse:~/workspace$

Tried console.log(response.data); to debug the data object and got undefined. Maybe the API has changed?

4 Answers

Peter Smith
Peter Smith
12,347 Points

Thats weird! What string exactly are you passing for username... Status Code 301 is Permanent Redirect.

Try maybe changing http:// to https:// ...just a guess though!

Sean Henderson
Sean Henderson
15,413 Points

You're on point! Was getting successful logs of the chunks by changing the url to http://www.google.com, but only a blank line from any treehouse url.

It's because teamtreehouse.com has implemented a https redirect for any requests to http, hence the 301 and blank body.

Changing the request url to https makes node complain about protocol, so the fix is to use the https module instead of http and change update the profile url to "https://teamtreehouse.com/username.json".

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

and

var request = https.get("https://teamtreehouse.com/" + username + ".json", function(response){
    // ...
}
Peter Smith
Peter Smith
12,347 Points

Oh nice! It all makes sense! I'm sure that Andrew Chalkley might want to add that to the teacher's notes or even re-record that video because its bound to trip a lot of people up.

I'm glad my guess could help you to find the answer. :)

Thanks for the great answer!

thanks!!