Welcome to the node.js in action article series. In this series we are talking about node.js and seeing various examples of it. You can visit the previous articles of this series here:
Fine, if you are a regular reader of this series then I hope you have understood the basic idea of node.js and want to do something really fun with it. But hold on, a few important concepts remain to be explained. One of them is module.
From my little experience in node.js, I can say that a module is one of the important concepts to be understand if you do not yet. The reason is, when we use node in a realistic scenario, we will see that everything in node will come as a module. So, let's start with an explanation of what is a module.
If you are an experienced programmer then I hope you understand the concept of modular programming. Yes, it's a methodology when one big application divides into a small chunk.
The same is true for a node.js application. In node.js, we can distribute the source code into several files and if needed, we can use them with the main application. It provides the ability to maintain code in a realistic environment.
So, let's create a simple module to understand the concepts. Create one .js file and put the following code into it. Save the file by giving the name “message.js” in the same location where you have kept the server file.
exports.welcome = function () {
console.log('Welcome to our application')
}
Here we would like to create one message module that can be included with the main server application. The function is attached with the “welcome” variable and again that is attached with “exports”. It ensures that the function can access from the outer world.
Now we will include this module in the main server program. Have a look at the following code.
var http = require('http');
var message_mod = require('./messageModule')
var server = http.createServer(function (req, res) {
message_mod.welcome();
});
server.listen(9090);
console.log('server running...')
In upper portion of this code, we have included this module using the "require()" function. Please note that it's not necessary to provide the file's extension.
Within the server function we are just calling the function from the messageModule module. Once we run this application, we should see the following output.
We are getting a message from the messageModule. The concept is, not only a message, we can keep a variable also in any module. Let's see that in another example.
We have changed messageModule code like the following.
multiplyBy = 5;
exports.multiply = function (val) {
return val * multiplyBy;
}
Have a look that, we have kept one variable in this module. The “5” is assigned to this variable and within the function we are doing the multiplication and returning the result. Now, we will modify our server.js application, here is the modified code.
var http = require('http');
var message_mod = require('./messageModule')
var server = http.createServer(function (req, res) {
console.log('The value by which multiply:-' + multiplyBy);
console.log('Result:-' + message_mod.multiply(100));
});
server.listen(9090);
console.log('server running...')
When we load this module using the require() function, it's behaving as a native code in the main application. So that we are able to access the variable by name.
Here is the output of the above example.
ConclusionIn this small article we have understood the concepts of modules in node.js. I hope you have understood the demonstration. In the next article we will understood a few more new concepts. Happy node.js learning.