Node.js quick start for Deep dive — Part 1

I have been learning Node.js for few months. Initially, I found that the docs had more of API modules briefly explained and going through all was not necessarily needed for starters, So I pulled out few notes on some specific modules.
The best way to learn NodeJs is through REPL. Typing node in your CLI will get you into REPL. Taping on TAB key twice gets the auto-suggest displaying all modules and API available in Node.js.

nivendha@HomePC:~$ node
>

let’s start on to cover with few of these.

1.Modules

All files in Node.js have to be loaded first using the require() which is available in global. Calling the require on core modules gets loaded immediately from the node itself.For a non-core module, the require() searches through the file specified in the list provided by the module’s paths

> module

Files are searched hierarchically from the current directory way down to the root directory. If it’s not able to locate the file, then we will get a Cannot find module error trace.

> require('./helloWorld.js')
Error: Cannot find module './helloWorld.js'
at Function.Module._resolveFilename (module.js:470:15)
at Function.Module._load (module.js:418:25)
at Module.require (module.js:498:17)
at require (internal/module.js:20:19)
at repl:1:1
at ContextifyScript.Script.runInThisContext (vm.js:23:33)
at REPLServer.defaultEval (repl.js:339:29)
at bound (domain.js:280:14)
at REPLServer.runBound [as eval] (domain.js:293:12)
at REPLServer.onLine (repl.js:536:10)

Adding a package.json to your local folder, you can load it just by giving the folder path.

# ./learnNodeJs/src/code/package.json
{
"name":"myCode",
"main":"run.js"
}

The modules have an one-to-one correspondence in Node. Requiring a module/file goes through the following process.

  • Resolving
    In this the absolute path of the file is located. This can also be used to check if the file exist in case of optional dependency
#index.js
var message = "Hello World";
module.exports = function(){
return message;
}
  • Wrapping
    Now the module/file is wrapped around a wrapper function, thus providing a scope for the module.
(function(exports, require, module, __filename, __dirname) {
var message = "Hello World";
module.exports = function(){
return message;
}
});
  • Evaluating and Caching
    Now the file gets loaded and cached for any future exports, next call to require loads the cached file.

2.Events

This plays the bread and butter for all Classes extending the EventEmitter. It provides the ability in Node.js to extend event listener for objects.

const EventEmitter = require('events');

The on() method takes an eventName and listener function to be triggered when the corresponding event is raised.

myEmitter.emit('myEvent',"myEvent is triggered"); //

The listeners are executed synchronously in order which they were registered. To switch to an asynchronous mode of operation we can use the setImmediate() or process.nextTick() wrapped over our callback code. The this reference inside the listener is set to EventEmitter.

Upcoming modules

  1. FS module
  2. OS module
  3. Util module
  4. Child Processes

#devloper ,likes art, origami and instrumental music

#devloper ,likes art, origami and instrumental music