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

Peter Retvari
seal-mask
.a{fill-rule:evenodd;}techdegree
Peter Retvari
Full Stack JavaScript Techdegree Student 8,392 Points

Can anyone explain EventEmitter in the profile.js file? There is another same question in the forum, without answer :)

As I can see it EventEmitter is an object of event built-in module. We can create an event emitter instance and fire an event (error, end, data) to run a function. Am I right? But this code is a little bit messy for me and I'm confused. What are we doing here?

function Profile(username) {

    EventEmitter.call(this);

    var profileEmitter = this;

This refers to our object itself (the profile object we get from the API, right?)?

I don't understand this part as well:

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

It seems like a duplicate for me. With response.on we already added the chunk data to the body.

Here I also confused: why we inherit profile and eventemitter and what is the other module?

util.inherits( Profile, EventEmitter );

I think its ridiculous that this course relies so much on unexplained code that is just copied and pasted. How are you supposed to become job ready if you only get the very basics explained and copy and paste the rest? I looked at that function and barely understood anything about it.

1 Answer

Peter Retvari
seal-mask
.a{fill-rule:evenodd;}techdegree
Peter Retvari
Full Stack JavaScript Techdegree Student 8,392 Points

I already found an explanation to util module. It works like a hybrid stuff. If we use util then any person will inherit the eventemitter so we can attach custom events for treehouse students. But I still don't get why we need eventemitter. In the previous lesson we didn't use it and we were able to handle the certain outcomes like (data, end, error). Why we need now?