The basics of NodeJS

With the basics of JavaScript out of the way, let's focus on some of the basics of Node.

Event driven

At its core, one of the most powerful features of Node is that it is event driven. This means that almost all 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 off other code listening for that event).

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

mongoose.connect('mongodb://localhost/MyApp'),
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.

Asynchronous

Earlier, we reviewed the idea of asynchronous JavaScript code in the browser using setTimeout—the principles apply even more in the world of Node. As you might be making a number of network-dependent connections to different REST API services, database servers, and anything else, it's important that your code can execute smoothly and has proper callback usage in place whenever each service responds.

Require and modules

In an effort to make code as modular and reusable as possible, Node uses a module system that allows you to better organize your code. The basic premise is that you write code fulfilling a single concern and export this code as a module that serves that single purpose. Then, whenever you need to use that code elsewhere in your code base, you would require that module:

// ** file: dowork.js
module.exports = {
  doWork: function(param1, param2) {
    return param1 + param2;
  }  
}

// ** file: testing.js
var worker = require('./dowork'), // note: no .js in the file

var something = 1;
var somethingElse = 2;

var newVal = worker.doWork(something, somethingElse);
console.log(newVal);
// => 3

Using this system, it's simple to reuse the functionality in a module (in this case, the dowork module) in any number of other files. Furthermore, the individual files of a module act as a private namespace. Any variables declared and used within the module file are private to that module and not exposed to any code that uses the module via require().

This system extends infinitely as well. Within your modules, you can require other modules and so on and so forth.

The NodeJS core

The NodeJS core literally has hundreds of modules available for you to use while writing your applications. These include the following:

  • Events
  • Filesystem
  • HTTP
  • Net
  • Streams
  • Timers

Definitely make sure to check out the online docs on Node (at http://nodejs.org/api) to see the full list of modules available in Node's core and see plenty of sample code and explanations.

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

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