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

Sean Henderson
Sean Henderson
15,412 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 (http://teamtreehouse.com/username.json
var request = http.get("http://teamtreehouse.com/" + username + ".json", function(response){
  //Read the data
  response.on("data", function(chunk) {
    console.log("BODY: " + chunk);
  //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

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,412 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";


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!