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 trialFrank Collins
12,090 PointsWhy is it necessary to call the EventEmitter class when creating a new event listener in Node.js?
I was looking through the code from Treehouse's Node.js course when I found something interesting.
The EventEmitter class was called within the profile object. I assume this is so that one would be able to use the class'es emit method indirectly to create a new error event. What confused me is why do this and have the Profile object inherit the EventEmitter class later in the code.
Another thing that I found was that when I commented out the call function the code worked normally. But when I did the same for the inherit function I got an error saying that profileEmitter.emit was not a function.
If this is the case what is the use for the call to the EventEmitter class?
var EventEmitter = require("events").EventEmitter;
var https = require("https");
var http = require("http");
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;
1 Answer
Seth Kroger
56,413 PointsEventEmitter.call(this)
is calling the EventEmitter constructor to do any initialization EventEmitter needs but it doesn't set the prototype which is needed to bring the methods over. That second part is with util.inherits does.
If you go back and review Object-Oriented JavaScript, this video in particular, you'll see the same thing being done. Just that util.inherits(Profile, EventEmitter)
does the same thing as Profile.prototype = Object.create(EventEmitter.prototype)
.