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

Jan Maas
Jan Maas
2,282 Points

Node.js Basics: Why am I not getting the same response output in the command line?

hi there :) I have this problem: I am getting a response from the teamtreehouse API, my console.log('response.statusCode') prints to the console fine, so does the response.on("end", ....) event. So I assume the http.get request is working.

However my response.on('data', function(chunk) { ....}); doesn't print anything to the console. I would like it to do the same as it does in the tutorial-video please...

Can anyone help? thanks :)

my code below:

var http = require("http");

var username = "janmaas";

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

// connect to API url [http: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);
    });

    response.on('end', function() {
        console.log('No more data in response');
    });

    // Parse the data
    // Print the data out

});


request.on("error", function(error) {
    console.error(error.message);
});

2 Answers

Seth Kroger
Seth Kroger
56,413 Points

What was the status code? Was it 200 (the status code for OK) or something else? Not all responses will have a body because it doesn't make sense with some statuses. (Like redirecting you to a different page)0.

https://teamtreehouse.com/community/301-error-with-http-not-https

Jan Maas
Jan Maas
2,282 Points

Hi, and thanks for your help Seth! I had indeed a different status code before (301 if I remember correctly)

Adding the https require and switching everything over to https works beautifully (at least for now) thanks again ! J