Better Understanding Node.js

Node.js is a server-side software system designed for writing scalable Internet applications, notably web servers. Programs are written on the server side in JavaScript, using event-driven, asynchronous I/O to minimize overhead and maximize scalability. It enables web developers to create an entire web application in JavaScript, both server-side and client-side.

Modules

Node has a simple module loading system. In Node, files and modules are in one-to-one correspondence.Think about it this way: In C++, you use classes or what have you to build your project. Then you have a compiler that comes along and bundles everything up for you into an assembly based on which classes you have declared you will be needing. Now you have a module that you can use in other projects, and it can easily be referenced or imported for use.

Module.exports and Exports

Before explaining the details, we should get some idea of module.require() method. The module.require method provides a way to load a module as if require() was called from the original module, and it return module.exports object from the resolved module.

Example 1: Exporting as an Object of functions

// make_sandwich.js
var fridge = require ('./fridge'); // load fridge module
function makeSandwich() {
    return {sandwich: fridge.bread() + ' ' + fridge.egg};
}
console.log(makeSandwich()); //output would be===>  { sandwich: 'bread: 2 egg: 1' }
//end of make_sandwich
//here is the fridge.js
exports.bread = function bread() {
    return 'bread: 2';
};
exports.egg = 'egg: 1';//end of fridge.js

Behind the scene, it is really creating an exports object in the fridge module:

// fridge.js
//var exports = {};
exports.bread = function bread() {
    return 'bread: 2';
};
exports.egg = 'egg: 1';
//exports = {
//     bread: function bread() { ... },
//     egg: 'egg: 1'
//};

Example 2: Exporting with module.exports so that we can do something like this:

var something = require( './something' );
something();

// bread.js
module.exports = function() {
    return 'bread: 2';
};
// make_sandwich.js
var bread = require ('./bread');
function makeSandwich() {
    return {sandwich: bread() + ' with some eggs'};    // directly call bread();
}
console.log(makeSandwich());       // output====> { sandwich: 'bread: 2 with some eggs' }

Working with Callbacks

Callbacks are essential for understanding how to program efficiently with Node.js. Callbacks are essentially just functions that execute once a method completes. It’s considered standard to create callbacks with at least two parameters: the global Error object, and the result of the asynchronous method completing.

// You call async functions like this.
someAsyncFunction(param1, param2, function callback(err, result) {
    if (err) {
        console.log("There's an error, and it reads: " + err);
    }
    else {
        doSomething(result);
    }
});

//Or, You call async functions like this.
// Do not put and arguments in the following callback, it won't work!!! If you have to, you can refer to the previous someAsyncFunction.

someAsyncFunction(param1, param2, myCallback);
function myCallback(err, result){
    if (err) {
        console.log("There's an error, and it reads: " + err);
    }
    else {
        doSomething(result);
    }
};
This entry was posted in open-source. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s