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

I'm leaving the course right now, the profile.js section not being explained is really confusing.

The profile.js file not being explained is really confusing so I leave this section for now

Nicholas Gaerlan
Nicholas Gaerlan
9,501 Points

I'm with you on this... This is entirely too focused on making a project that utilizes a featureset node.js vs explaining node.js on the whole. I'd be more interested if maybe there was a more technical breakdown of the most used features of node.js FIRST, and then an implementation of those features. Feels like the context to all of this is just... missing. I'm looking around online for other sources to learn node.js. Youtube has some stuff, and pluralsight has some stuff too. The vids with Cloud 9 IDE are nice because you don't have to setup anything on your machine and it works like workspaces. I've been thinking about the JS courses here. I think it would be better to learn JS the ES2015 way first and glance over the old way of writing it, but focus on ES2015 and probably JQuery as the beginner course. Then the full stack course should spend a huge chunk of time explaining Node (since it's so important) and then move onto Express, Mongo, and React. After all of that, I would want to see projects that build from the ground up, several web applications that utilize the full stack, instead of piece meal applications that show you how to try out bits and pieces. Personally, I've always done better just diving into the deep end first, seeing the big picture and the breaking it down. It's easier to understand the role of the small pieces if you have the larger context of a big picture in the back of your mind.

Steven Ventimiglia
Steven Ventimiglia
27,371 Points

Yeah, this course is worthless imo. It's like something magical is happening and I honestly don't care how any of this works if there's zero explanation of what 'profileJSON' is.

So, according to what I've been taught so far: We're now pulling data out of thin air, and I can throw 'profileJSON' into any function I want to, since it will pull a JSON file magically from anywhere I want it to.

Horribly disappointing, and in no way a positive approach to motivate me when it comes to learning Node.js.

4 Answers

Hi Gianni,

I understand your confusion. I went through the same issue and I was annoyed that there was no section that actually explains EventEmitter (in profile.js) in detail. I say that because EventEmitter is a big feature of Node.js and actually is the core of several core Node.js modules. However, I encourage you to look at the profile.js file yourself and try to make sense of it. I have found to love creating my own EventEmitter as you can create some awesome stuff with them. In this video Profile is just a custom EventEmitter that can emit its own custom events. Let me explain by creating our own little event emitter:

Imagine this is in file: MyOwnEmitter.js

//Here at the top of the file you are requiring some core modules from node.js
//what it does: it 'imports' some core functionalities from node.js
//EventEmitter: The base 'class' for your  'custom' event emitter
var EventEmitter = require("events").EventEmitter;
//https: Allows you to make https calls to other resources (here our own made up api)
var https = require("https");
//util: is used so you can extend EventEmitter and create your own 'child' class of it.
var util = require("util");

//Now create our own 'class constructor'. We do this so later in our application we can instantiate our eventemitter.
function MyOwnEmitter() {
      //Call the Node.js EventEmitter constructor (you need to do this so you can inherit from it later)

      //We create a local variable 'self' which just refers to itself so we do not create any scoping issues when doing our api call
      var self = this;

      //let's do our api call to some made up api that returns weather data, by using the https module from above.
     //first parameter: the url we are requesting. second parameter: a callback function to run after the request is made
      var request = https.get('https://someMadeUpWeatherApi/', function(response) {
                     //set the body just to an initial string initially
                     var body = "";
                     //Here we listen for the 'data' event and add to the body as the data is coming to our server
                           body += chunk;
                           self.emit('dataFromMyApiIsHere', chunck);

                    //Here we listen for the 'end' event which is fired once our response is done. Which means we can parse the data
                   response.on('end',function() {
                         //parse the body into json so we can easily read it wherever we want to later
                         var json = JSON.parse(body);
                         self.emit('endOfOurApiData', json);


//Here we inherit from EventEmitter so our 'class' MyOwnEmitter can use all of the same methods as EventEmitter. like //'emit' and 'on' as we have already seen above in self.emit etc.
util.inhertis(MyOwnEmitter, EventEmitter);

//export our class/module so we can use it in other files
module.exports = MyOwnEmitter;

Imagine now this is in the file app.js

var myOwnEmitter = require('./MyOwnEmitter.js');

//now we instantiate our own emitter;
//What happens at this moment -- it runs through everything we put in the function MyOwnEmitter above. 
//Including the api call to our made up weather api
var myOwnEmitterInstance = new myOwnEmitter();

//Now we can listen for the events 'dataFromApiIsHere' and 'endOfOurApiData' we emitted above

         //this will actually be all of the data our weather api returned in json format!!!

EventEmitters are cool and they are everywhere in node.js. Even the response object we saw in the https request is an EventEmitter that emits 'data' and 'end' events we can listen to. Node.js is event driven which makes it one of the reasons why it is so so fast. The above is a fairly common design pattern in node.js which is called the observer pattern I believe.

thanks for this

Visal Perera
Visal Perera
9,716 Points


watching this above video before this confusing treehouse section is what turned night into day for me, even though the video is an hour long, take a breath and slowly watch through it, and node becomes your friend. Another point to make is that once you watch the youtube video you will notice that treehouse is using older practices when defining the Profile class and when extending EventEmitter classes (dont worry you will know what I mean after watching the youtube video)

Akash Sharma
Akash Sharma
Full Stack JavaScript Techdegree Student 14,147 Points

Hi Alexander La Bianca thank you for your explanation.

Basically would the made up API on their server also emit an event called 'data' and 'end' that sends an argument back called 'chunk' that we append to our own variable - body?

Hi Akash, you get the point, but the made up API is not what emits the event 'data' and 'end'. It is the response object we get in the callback of the https.get method. The Node response object under the hood actually extends the EventEmitter just like we did with the MyOwnEmitter class. The response object is a writeable stream and emit a 'data' event whenever data is written to it. We can then listen to that 'data' event to extract the data the API server is writting to it. Once the API has written all the data to it, the Response object will emit one more event - 'end' to let us know we now have all the data.

Carlos Braga
Carlos Braga
13,587 Points

Hi Nicholas Gaerlan. You might have done already but if not, watch the course Node.js Basics first. https://teamtreehouse.com/library/nodejs-basics-2 and do this one. It will make a lot of sense. Node.js Basics gives you a quick fundamental and also help you to create the structure of the profile.js