Event-driven

At its core, one of the most powerful features of Node is that it is event-driven. This means that almost all the code you write in Node is going to be written in a way that is either responding to an event or is itself firing an event (which in turn will fire other code listening for that event).

Let's take a look at the code that we'll write in a later chapter that handles connecting to a MongoDB server using Mongoose, a popular Node.js MongoDB Object Document Mapper (ODM) module:

mongoose.connect('); 
mongoose.connection.on('open', function() { 
console.log("Connected to Mongoose..."); 
}); 

First, we tell our mongoose object to connect to the server provided as a string parameter to the function. Connecting will take an undetermined amount of time though, and we have no way of knowing how long. So, what we do is bind a listener to the open event on the mongoose.connection object. With the use of the on keyword, we are indicating that when the mongoose.connection object triggers an open event, it executes the anonymous function that was passed in as the parameter.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.135.183.138