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

The inner workings of profile.js aren't explained at all in this course segment :(

I feel as if every part has been covered well except one of the core parts to this course segment, i.e the profile.js file. I know in the last segment we covered the http.get + https.get and grabbing from an API but the syntax in profile.js is a lot more complex and most if it not explained at all previously.

Additionally this is creating a lot of confusion as the variable studentsProfile is creating a new instance of Profile(username) and then on studentProfile.on we have a end event function taking in (profileJSON) that isn't declared. This initially for me caused a lot of confusion as a result of the inner workings of profile.js not being explained.

It would have been a lot better if the course flow allowed us to build our own profile.js AND THEN use that in this segment -- instead we just totally skipped that and went straight into building based on the unknown.

I really liked the way Andrew delivered this segment of the course; there's little bits and pieces that he neglected to explain but with some research it was easily found. It's just somewhat annoying building an app based on something you know little about! :P

7 Answers

James Barrett
James Barrett
13,253 Points

Hi there,

I'm still slightly confused about the profile.js file. It seems to be everything we covered in the last section, with the exception of the Event Emitter. What exactly is this doing and how is this benefiting our program? I tried searching the docs but couldn't truly understand the importance.

Thanks, James.

israel meza
israel meza
Courses Plus Student 10,890 Points

I agree. This is the first time in any of treehouse courses where I feel lost.

The EventEmitter is the "class" or "object" that we are inheriting methods from. To do this we are using

util.inherits( Profile /* child constructor*/, EventEmitter /* parent constructor*/);
//&&
EventEmitter.call(this)

var profileEmitter = this;

This allows us to access EventEmitter methods and the profileEmitter allows us to refer to "this" within anonymous functions that we have created ("this" within an anon function or "callback" refers to the anon function and not the parent function that it is nested in) The EventEmitter allows us to expose events for the Profile function (or class) using the "emit" method.

profileEmitter.emit("eventName", eventDataForCallBack);

this means that with our studentProfile we can use the "on" method to tie a callback function to the "eventName" we specified

studentProfile.on("eventName", function(eventDataForCallBack) { 
    console.log(eventDataForCallBack); 
});

However nowadays we have ES6 classes so we dont need to do this loophole anymore, we can just extend the class like so

var EventEmitter = require("events").EventEmmiter

class Profile extends EventEmitter{

}

and this will allow "this" to be used to expose the eventemitter methods

Mr.Chalkers, please respond to these messages.

Franck DAKAYI
Franck DAKAYI
9,297 Points

I agree too. profile.js needs some explanation...

Patrick Perkins
Patrick Perkins
14,619 Points
    var studentProfile = new Profile(username);

    studentProfile.on("end", function(profileJSON) {
      //show profile

      //store the values we need
      var values = {
        avatarUrl: profileJSON.gravatar_url,
        username: profileJSON.profile_name,
        badges: profileJSON.badges.length,
        javascriptPoints: profileJSON.points.JavaScript
      }
      //simple response
      response.write(`${values.username} has ${values.badges} badges\n`);
      response.end('Footer\n')
    });

What is being stored in the studentProfile variable? What is being passed into the profileJSON parameter on the callback function? I understand that it is a JSON object but profileJSON is never declared. Where does it come from and how is it automatically passed into the callback?

A video explaining the inner workings of profile.js could help clear up some of the confusion here.