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 Build a Simple Dynamic Site with Node.js Handling Routes in Node.js Populating User Information

Ben McMahan
Ben McMahan
7,317 Points

profile.js crashing when checking status code

There's one line that seems to be crashing everything in profile.js -

profileInstanceContext.emit("error", new Error("There was an error getting the profile for " + username + ". (" + http.STATUS_CODES[response.statusCode] + ")"));

If I comment this line out, everything works. Here is the complete profile.js file, it was originally copied from the forum as Andrew's version was so deprecated.

const EventEmitter = require("events");
const https = require("https");
const http = require("http");
const util = require("util");

class Profile extends EventEmitter {

    constructor(username) {
        super();
        // need for  calling the  methods of EventEmitter
        let profileInstanceContext = this;

        const request = https.get("https://teamtreehouse.com/" + username + ".json", function(response) {
            let body = "";

            if (response.statusCode !== 200) {
                request.abort();
                //Status Code Error
                profileInstanceContext.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;
                profileInstanceContext.emit("data", chunk);
            });

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

    //Connect to the API URL (https://teamtreehouse.com/username.json)
}

module.exports = Profile;

Stack trace:

Emitted 'error' event on Profile instance at:
    at ClientRequest.<anonymous> (C:\Users\Benjamin\WebstormProjects\dynamic\profile.js:24:28)
    at Object.onceWrapper (node:events:510:26)
    [... lines matching original stack trace ...]
    at TLSSocket.Readable.push (node:internal/streams/readable:228:10)