How can create Module in Node.js and reference them in any js file?

Hey guys,

Node.js same as other programming languages supports module. Basically modules help us to have better structure for our application and also it let you to maintain your application easily. For example you have an application that are doing these tasks:

1. Send email
2. Grab files from FTP
3. Create output files and upload into FTP
Each of these tasks can be a separate module, for those that are familiar with object oriented programing, module can play the same role as class. Module is not something new in Node.js but you must know how to define module and how import them to your app.js or anywhere that you want to use them.
Follow these few steps:
1. Create a js file in root folder. ( you need to have node folder, if you do not know what is node folder you can first look at this post)
2. Each module can have two types of function.
2.1. Public function which others can call this function and it is like interface for the module.
2.2. Private function which is used by other functions inside the module and others from outside cannot use it.
3. Copy can past these code inside the js file.

var name;




exports.getname = function(inputname){
     name=inputname;

}


exports.Printname = function(){
   console.log(name);

}

This is a very simple example of module, in this module we have a global variable which is name and two functions, first is getname which gets name and the other one is Printname which prints out the name. To make a function public we need to export it.

How can call a module?
It is very easy, first you need to import your module, in this example I want to import the module in app.js file but it can be imported in any other js file, based on your need. To impost any module you must use this command:


var testmodule = require("./getname.js");

require is something like import in vb.net or using in C#, if your module installed inside the node_modules folder, then you do not need to mention any path an straight you can name the module. But if your js is in the root then you can access to it thru this address:

(“./getname.js”) // Name can be anything based on your module name

“Require” creates an object from the module, so after that you can access all exported function via that object:


testmodule.getname('Mehran');
testmodule.Printname();

I hope this example helps you to understand Node module better, Node.js is like a puzzle you need to spend time to understand the whole story, and it may not be easy as the environment and concept is a bit different with what we used to it.

you can download full set of code form my github :

https://github.com/CodingTips/tutorial

for any questions you can contact me through my email :
janfeshan.mehran@gamil.com

bye bye



Categories: Node.Js

Tags: ,

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: