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 code not working

Following through with the Node.js tutorial, and got to the step to chain all the response.on('data') chunks together for one response. My code is here:

/* requires all needed modules */
var http = require("http"); 

/* Makes a request to teamtreehouse's user API, and handles any errors */
function makeRequest(){
    var request = http.get("http://teamtreehouse.com/michaeldu2.json", function(response){
        var body = ""; 
        response.on('data', function(chunk){
            console.log(chunk); 
        });
        response.on('end', function(){ 
            console.log(body); 
        })
    }).on("error", function(err){
        console.log("following error occured: "  + err.message); 
    });
}

makeRequest(); 

However, console.log(body) comes out as just "" everytime. I tried putting a console.log inside response.on('data'), but that never gets called. Can't figure out why this is happening. I'm running this on my localhost by the way, and each step in the tutorial was working fine and I get a response object, just response.on('data') never triggers. Thanks for your help.

2 Answers

Hi Michael,

In October, Treehouse switched to a HTTPS protocol; this is probably what's causing your error. Look at the Teacher's Notes here to learn more. In the meantime, your code above should work if you change all http references to https — like so:

/* requires all needed modules */
var https = require("https");

/* Makes a request to teamtreehouse's user API, and handles any errors */
function makeRequest() {
    var request = https.get("https://teamtreehouse.com/michaeldu2.json", function (response) {
        var body = "";
        response.on('data', function (chunk) {
            console.log(chunk);
        });
        response.on('end', function () {
            console.log(body);
        })
    }).on("error", function (err) {
        console.log("following error occured: " + err.message);
    });
}

makeRequest();

Ah, that fixed it. Thank you!