Skip to main content

What is module in Node.js?

A set of functions you include in your node.js application, that is a module. Modules can be either be single files or directories containing one or more file. Let's see in detail

Node.js Modules

Consider modules to be the same as Java-Script libraries. A module encapsulates code into a single unit of code.

Whenever you create any type of functions / objects in node.js and you reuse the these functions/objects , then these process you can called module.

  • For example : Mathematical calculation, Fatch the data from database and any type of task you want to perform , then you need create a module.

Type of modules in node.js.

  • Built-in Modules

  • Custom modules (User defined modules).

Note : When ever if we don’t create any Node.js modules ourselves, then we already have modules at our disposal because the Node.js environment provides so many built-in modules for us.

# roles inside of Node.js

The Module has two-main roles inside of Node.js.

  • The module’s First, it provides a foundation for all Node.js modules to build off. Each file is given a new example of this base module on load, which persists even after the file has run. This's why we are able attach properties to "module.exports" and return them later as needed.

  • The module’s second big job is to handle Node.js module loading mechanism. The require() function that we use is actually an abstraction over module.require, which is itself just a simple wrapper around Module._load. This load method handles the actual loading of each file, and where we’ll begin our journey.

1. Built-in Modules

Node.js has a set of built-in modules which you can use without any further installation.Built-in Modules is provided by the system.

  • Built-in modules is also called Core modules.

  • The Built-in modules are defined within Node.js source and are located in the "lib/" folder.

  • The Built-in modules has several modules compiled into the binary and load automatically when Node.js process starts.

  • Built-in modules are always preferentially loaded if their identifier is passed to require(). For example, require('http') will always return the built in HTTP module, even if there is a file by that name.

=> How to Include Built-in Modules

The module is implemented in the require('module') function, by using require() function you include the Built-in module in node.js

 ➤ Example: Include the "http" built-in module

var http = require('http');

Now your node.js application has included and access to the HTTP module, and is able to create a server:

 ➤ Example: Include and Use http Module

var http = require('http');
http.createServer(function (req, res)
  {
     res.writeHead(200, {'Content-Type': 'text/plan'});
     res.end('Hello Node.js!');
  }).listen(8080);

2. Custom modules

Custom modules is created by user, by using exports keyword and use it in your application.

  • It is also called User defined modules, File modules or Own modules.

  • With the help of the exports keyword to make properties and methods available outside the module file.

=> How to create your Custom modules

  You use exports keyword to create the Custom module in node.js

 ➤ Example: mymodule-file.js

    Create a module that returns the value of add() function

exports.add= function(x,y)
  {
    return x + y ;
  }

=> How to Include Custom modules

require('module') function also used to include Custom module in node.js

 ➤ Example: index-file.js

    Use the module "mymodule-file.js" in a Node.js file

var http=require('http');
var module_object= require('./mymodule-file');//Here included mymodule-file.js
http.createServer(function (req,res){
  res.writeHead(200, {'Content-Type': 'text/plan'});
  res.write("mymodule-file output is : "+module_object.add(25,60).toString());
  res.end();
}).listen(8080);

Comments

Popular Posts

How to remove the date and .html from every blogger post url

#Remove date and .html from blogger post url A Common search term which every blogger search is How to Remove Date From Blogger Post URL or how do I remove date from blogger permalink? Follow the steps below and then date and .html will be removed from the URL of your blogger post. Step 1 : Login to your Blogger blog and select Theme / Template. Step 2 : Click on Edit HTML and paste the below code just above the </head> tag let's see code :   ➤ Code : mycode.js; Copy code <script type='text/javascript' > //<![CDATA[ // BloggerJS v0.3.1 var urlTotal,nextPageToken,postsDatePrefix=!1,accessOnly=!1,useApiV3=!1,apiKey="",blogId="",postsOrPages=["pages","posts"],jsonIndex=1,secondRequest=!0,feedPriority=0,amp="&"[0];function urlVal(){var e=window.location.pathname,t=e.length;return...

Django static files not working when debug false || debug true

# Django static and media files not working when debug is false In this article you will learn how to fix problem of not loading static files and media in Django even the DEBUG is FALSE. This is the easiest and safest solution. # Problem: Django static and media files not working when debug is false  ➤ Code: settings.py DEBUG = False #True ALLOWED_HOSTS = [ '*' ] #Host name # Problem Fix: Let's see, How you can fix the problem of Django static and media files not working when DEBUB = False : 1.)First way: devserver in insecure mode If you still need to server static locally ( e.g. for testing without debug ) you can run devserver in insecure mode: python manage.py runserver --insecure --insecure: it means you can run serve...

How to remove ? m=1 or ?m=0 from blogger post URL

# Remove m=1 From URL of Blogger post A Common search term that every blogger search is How to ?m=1 or ?m=0 from blogger Post URL. We all know that "simplicity is beauty" and you want to clean permalink. So, in this article, I will guide you on how to remove ?m=1 from the blogger URL, and make a simple professional URL. Follow the few steps below and removed ?m=1 from the URL of your blogger post. Step 1 : First, you login into your blogger's dashboard and then select your blog. Step 2 : Click on the Theme option. Step 3 : Click on the customise Step 4 : Click on Edit HTML option. Step 5 : Press (CTRL + F) from the keyboard and type "/head" and then search. ( If you are not understanding, see the below photo ) Step 6 : Now paste the below code just above the </head> tag. let's see code :   ➤ Code : mycode.js; Copy code ...