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

Pascal Klau
Pascal Klau
15,748 Points

profile.js has been edited behind the scenes.

I started this project after the NodeJS basic course and copied my profile.js file which I created during this course into this one.

Using my JS file is always received the error "Profile is not defined." This is because the profile.js in Workspace has new content in it. It requires "util" and "events" which aren't explained anywhere.

So no way to learn about this during this course?

/rantOver

2 Answers

rydavim
rydavim
18,814 Points

You are correct that these additional lines of code are not addressed in this course, and this has been brought up before.

You can find the documentation on util on this page, and the documentation for EventEmitter on this one.

I'll try to do a short explanation of my understanding here, and hopefully someone will correct me if I miss something.

util.inherits() is used here to inherit the prototype methods from EventEmitter into Profile.

At this point, it gets a little bit trickier to follow.

When EventEmitter is called in the Profile instance creation, it appends properties from the EventEmitter constructor to Profile.

// Try using these console.log statements to see what's happening.
console.log(this);
EventEmitter.call(this);
console.log(this);

After that the profileEmitter.emit() method is used to trigger events, passing arguments to listener functions. This lets us handle things like incoming data chunks and errors.

// Triggers an event that adds the data chunk that was just received to body.
profileEmitter.emit("data", chunk);

// Sends an error event, along with the error from catch.
profileEmitter.emit("error", error);

Hopefully that helps explain the changes to the profile.js file, but let me know if you still have questions. Happy coding! :)

Andrew Chalkley
STAFF
Andrew Chalkley
Treehouse Guest Teacher

Yep it's a different profile.js file, with an example of it's usage. Think of it as a 3rd party library or a black box where only the interface (the external API) is exposed to you. If you notice it's got a similar interface to the http client for retrieving data, so I wanted there to be some familiarity with it's API for asynchronous data calls. For more explanation look here.