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 trialSean Henderson
15,413 PointsNot 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
12,347 PointsThats 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
15,413 PointsYup, thanks Peter!
Jisoo Shin
1,632 PointsThanks for the great answer!
Christiane Deneser
4,303 Pointsthanks!!
Sean Henderson
15,413 PointsSean Henderson
15,413 PointsYou'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".and
Peter Smith
12,347 PointsPeter Smith
12,347 PointsOh 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. :)