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

http > https - how this change effects the rest of the tutorial?

Since treehouse urls changed from http to https, how will this effect this tutorial? Are there http methods we need to use that can't be used on https?

which tutorial?

I'm doing the "Build a Simple Dynamic Site with Node.js"

1 Answer

Maybe this other thread answers the question?

https://teamtreehouse.com/community/error-getting-profile

EDIT: I just did this myself. In the profile.js file, you need to keep the "var http = require("http");" but you you're also going to add "var https = require("https");". Then, in the same file, down where you're defining the Profile function, after it says "//Connect to the API URL... " you're going to change http.get to https.get and you're going to change the url to https:// instead of http://. But DON'T change "http.STATUS_CODES" to "https.STATUS_CODES". The way I understand this is that we're using the https protocol now, but I guess they didn't give us an array of status codes in the https protocol, so we still have to use the http object we created to access the http status codes in case there's an error.

Let me know if you need any more clarification. Here's my code:

var EventEmitter = require("events").EventEmitter;
var http = require("http");
var https = require("https");
var util = require("util");

/**
 * An EventEmitter to get a Treehouse students profile.
 * @param username
 * @constructor
 */
function Profile(username) {

    EventEmitter.call(this);

    profileEmitter = this;

    //Connect to the API URL (https://teamtreehouse.com/username.json)
    var request = https.get("https://teamtreehouse.com/" + username + ".json", function(response) {
        var body = "";

        if (response.statusCode !== 200) {
            request.abort();
            //Status Code Error
            profileEmitter.emit("error", new Error("There was an error getting the profile for " + username + ". (" + http.STATUS_CODES[response.statusCode] + ")"));
        }

        //Read the data
        response.on('data', function (chunk) {
            body += chunk;
            profileEmitter.emit("data", chunk);
        });

        response.on('end', function () {
            if(response.statusCode === 200) {
                try {
                    //Parse the data
                    var profile = JSON.parse(body);
                    profileEmitter.emit("end", profile);
                } catch (error) {
                    profileEmitter.emit("error", error);
                }
            }
        }).on("error", function(error){
            profileEmitter.emit("error", error);
        });
    });
}

util.inherits( Profile, EventEmitter );

module.exports = Profile;