Create, Publish, Extend & Manage
A module in Node.js is a logical encapsulation of code in a single unit. It is a good practice to always segregate code in such a way that it simplify creating manageable and maintainable for future purposes. Because of this we are creating module in nodejs
Since each module is an independent entity with its own encapsulated functionality, it can be managed as a separate unit of work.
What are modules in Node.js?
There are many modules available in nodejs
-
Express framework – Express is a minimal and flexible Node js web application framework that provides a robust set of features for the web and Mobile applications.
-
Socket.io – Socket.IO enables real-time bidirectional event-based communication. This module is good for creation of chatting based applications.
-
Jade – Jade is a high-performance template engine and implemented with JavaScript for node and browsers.
-
MongoDB – The MongoDB Node.js driver is the officially supported node.js driver for MongoDB.
-
Restify – restify is a lightweight framework, similar to express for building REST APIs
Creating Module
We can create custom module and include in Nodejs application, Nodejs support this feature.
In the below example we can create our own module and include it in our application
Step 1) Create a file called “add.js” and include the below code.
app.js
//1.Define a Module
var myExports = module.exports = {};
//2.Create a function in our Module
myExports.AddNum = function(a, b) {
//3. Returning a value back to the calling function
return a + b;
}
We have created our custom module, next we will call this moudle
The “exports” keyword is used to ensure that the functionality defined in this file can actually be accessed by other files.
main.js
// Using require to Include the Addition Module
var Addition = require('./app');
// calling the AddNum function in our module
console.log(Addition.AddNum(1, 2));
We are using the “require” keyword to include the functionality in the app.js file.
Since the functions in the app.js file are now accessible, we can now make a call to the addition function.
Now run the node command to execute the code “node main.js”
Output
node main.js
3
Example for Module,Exports,Require
one.js
------
var one = function() {
console.log('I am from one');
}
module.exports = one;
two.js
-------
var two = function() {
console.log('i am from two');
}
module.exports = two;
index.js
--------
var one = require('./one');
var two = require('./two');
module.exports = {
one: one,
tow: two
}
app.js
------
var both = require('./converter');
both.one();
both.tow();
Output :
I am from one
i am from two
Different Ways To Use module.exports
1.
details.js
----------
//exports is an javascript object , we can asign anything
module.exports = "Hello";
main.js
--------
var details = require('./details');
console.log(details);
//OutPut
Hello
2.
details.js
-----------
var name = 'John';
var getName = () => name;
module.exports = getName; // Passing Refrenence
main.js
--------
var details = require('./details');
console.log(details()); // Call the function
//OutPut
John
3. Best Way exporting
details.js
----------
var name = 'John';
var city = 'Mumbai';
var getName = () => name;
var getCity = () => city;
// Export JSON Object, here we add the logics before we export
module.exports = {
getName,
getCity
};
main.js
--------
var details = require('./details');
console.log(details.getName());
console.log(details.getCity());
//OutPut
John
Mumbai
4.
//The above main program can be written as below
var { getName, getCity } = require('./details');
console.log(getName());
console.log(getCity());
//OutPut
John
Mumbai
5.
//Directly Export function
details.js
-----------
var name = 'John';
var city = 'Delhi';
// Export Functions
module.exports.getName = () => name;
//We can attach any number of functitons
module.exports.getCity = () => city;
main.js
-------
var details = require('./details');
console.log(details.getName());
console.log(details.getCity());
//OutPut
John
Delhi