Wednesday, January 17, 2018

Node.js Modules

for Basic About Node JS: http://minervajeyarajah.blogspot.com/2017/05/node-js.html



A module in Node.js is a simple or complex functionality organized in single or multiple JavaScript files which can be reused throughout the Node.js application.



Consider modules to be the same as JavaScript libraries, a set of functions you want
to include in your application.


Module Types:

Node.js includes three types of modules:
  1. Core Modules
  2. Local Modules
  3. Third Party Modules

Node.js Core Modules:

Node.js is a lightweight framework.
The core modules include bare minimum functionalities of Node.js.
These core modules are compiled into its binary distribution and load automatically
when Node.js process starts.
However, you need to import the core module first in order to use it in your application.
The following table lists some of the important core modules in Node.js.
Core Module
Description
http module includes classes, methods and events to create Node.js http server.
url module includes methods for URL resolution and parsing.
fs module includes classes, methods, and events to work with file I/O.
path module includes methods to deal with file paths.


Loading Core Modules:

In order to use Node.js core or NPM modules, you first need to import it
using require() function as shown below.
var module = require('module_name');
As per above syntax, specify the module name in the require() function.
The require() function will return an object, function, property or any other JavaScript type, depending on what the specified module returns.
var http = require('http');
var server = http.createServer(function(req, res){
//write code here
});
server.listen(8080);


In the above example, require() function
returns an object because http module returns
its functionality as an object, you can then use its properties and methods using dot notation
e.g. http.createServer().
In this way, you can load and use Node.js core modules in your application.

Node.js Local Module:

Local modules are modules created locally in your Node.js application.
These modules include different functionalities of your application in separate files and folders.
You can also package it and distribute it via NPM, so that Node.js community can use it.

Writing Simple Module:

Let's write simple logging module which logs the information, warning or error to the console.
In Node.js, module should be placed in a separate JavaScript file. So, create a log.js file and write the following code in it.
log.js
var log = {
info: function (info) { console.log('Info: ' + info);
},
warning:function (warning) { console.log('Warning: ' + warning);
},
error:function (error) { console.log('Error: ' + error); }
}; module.exports = log
In the above example of logging module, we have created an object with three functions - info(), warning() and error().
At the end, we have assigned this object to module.exports.
The module.exports in the above example exposes a log object as a module.
The module.exports is a special object which is included in every JS file in the Node.js application by default.
Use module.exports or exports to expose a function, object or variable as a module in Node.js.
Now, let's see how to use the above logging module in our application.

Loading Local Module:

To use local modules in your application, you need to load it using require() function in the same way as core module.
However, you need to specify the path of JavaScript file of the module.
The following example demonstrates how to use the above logging module contained in Log.js.
app.js
var myLogModule = require('./Log.js');
myLogModule.info('Node.js started');
In the above example, app.js is using log module.
First, it loads the logging module using require() function and specified path where logging module is stored.
Logging module is contained in log.js file in the root folder.
So, we have specified the path './log.js' in the require() function.
The '.' denotes a root folder.
The require() function returns a log object because logging module exposes an object in log.js using module.exports.
So now you can use logging module as an object and call any of its function using dot notation
e.g myLogModule.info() or myLogModule.warning() or myLogModule.error()
Now you can run the application and can see the status in your terminal
Thus, you can create a local module using module.exports and use it in your application.
For more on Node Module : https://nodejs.org/api/modules.html


No comments:

Post a Comment

My First Day with Uki DN Cohort 1

 "Hello WORLD!"