Sunday, March 29, 2020

Node.js Modules

Node.js Modules

What is a Module?


A module in JavaScript can be visualized something like JavaScript libraries. A module is a set of functions that we may need to include in our application.

A module can be considered as a set of simple or complex functionality that can be contained in a single or multiple JavaScript files. A module can be reused inside the entire Node application.

Each module has its own context, so it should not interfere with other modules or disturb the global scope. All of the modules can be kept in a separate .js file under a separate folder.

We can categorize Node.js modules as,

  • Built-in Modules
  • Local Modules
  • Third-party Modules


Built-in Modules

There are several built-in modules available by Node.js that do not require any additional installation before use. Node.js is a lightweight framework. These built-in modules are the core modules and include bare minimum functionalities to enable Node.js. These core modules are compiled into its binary distribution and load automatically when the Node.js process starts. We still need to import the core module to make use of their functionality in our application. http, url, fs, querystring, path, util are the example of Built-in Modules.

How to add include Modules?

We can use the require() function along with the name of the module as an argument to include a module in a script.

For example

const http=require(‘http’); 

Now we can use the included module.

var http = require('http');
        http.createServer(function (request, response) {
          response.writeHead(200, {'Content-Type': 'text/html'});
          response.end('Hello World!');
        }).listen(8087);

Local Modules

The modules created locally in our Node.js application are called local modules.

Constructing our own Modules

We can construct our own modules (Local modules) to include and use them in our applications. Let’s create a module that simply returns a date and time object,

exports.myDateTime = function () {
             return Date();

};

The exports keyword is used to make properties and method defined in our module available outside the module file.
Save the script written above in a file as "my_module.js"


Example to include a Module


        const http = require('http');
        const date = require('./my_module');

        http.createServer(function (request, response) {
          response.writeHead(200, {'Content-Type': 'text/html'});
     response.write("<h3>Date and Tmie: " + date.myDateTime()+" </h3>");
          response.end();
        }).listen(8087);



We must use ./ to locate the module, it indicates that the module is available in the same directory as the Node.js document. 

Save the script in a file as "test_module.js", and execute the script: 


Start demo_module.js: 



C:\Users\user> node test_module.js 


If you have followed the same steps on your computer, you will see the same result as the example: http://localhost:8087

The result will be shown as,

result