Writing a single module in different files - Node.js

Lets say we have a module which contains code of 5000 lines and with time the module keeps on growing.

So eventually at some point we would have to split the module (at least for maintainability and readability) in different files without impacting the other layers utilizing this module.

So lets say the module is in "my_module.js" file. So out first step is to create a folder "my_module" and start splitting my_module.js file in smaller files in this folder and create an index.js file as well.

for example:
my_module
	-- my_module1.js
	-- my_module2.js
	-- index.js


Put all common and private methods in index.js file.

for example, index.js will look like...

var my_module = function(){
	
	//private variables
	var private_variable = 0;
	
	//private methods
	var isDateValid = function(date){
		if(isNaN(Date.parse(object))){
			return false;
		}
		return true;
	}
	
}


Now how to include other parts of my_module.

Each part of my_module.js file will accept my_module class (defined in index.js) object as parameter and when they are trying to reference a method defined in same file or any other file, then they should access the method using the same variable instead of this (except private variables and methods).

for example my_module1.js would look like:
var my_module1 = function(parent){ //parent is reference of my_module object
	this.Method1 = function(){
		return "hello";
	}
}	
module.exports = my_module1;


Now lets try to access Method1 defined in my_module1.js in my_module2.js file
var my_module2 = function(parent){ //parent is reference of my_module object
	this.Method2 = function(){
		return parent.Method1() + " " + "world"; //calling method1 using parent object, instead of "this"
	}
}	
module.exports = my_module2;


Now lets include these methods in index.js my_module object. To do so lets include an extend method, this method will extend the my_module object with my_module1 and my_module2 objects.

var my_module = function(){
	
	//private variables
	var private_variable = 0;
	
	//private methods
	var isDateValid = function(date){
		if(isNaN(Date.parse(object))){
			return false;
		}
		return true;
	}
	
}

//method to extend the object
var extend = function(target, source) {
	for (var i in source) {
		if (source.hasOwnProperty(i)) {
			target[i] = source[i];
		}
	}
}

my_module.instance = null;
my_module.getInstance = function(){
    if(my_module.instance === null){
        my_module.instance = new my_module();

		//list of files we have created
    	var files = [
			'my_module1',
			'my_module2'
		];
		
		var fs = require("fs");

		files.forEach(function(fileName){
			//you can pass any dependencies to sub files here
	        var fileObj = new (require('./' + fileName))(my_module.instance);
	        //adding fileObj object properties to the my_module
	        extend(my_module.instance, fileObj);
		});
    }
    return my_module.instance;
}

module.exports = my_module.getInstance();


Now lets say we want to have some hierarchy or name-space structure present as well.
Then we would extend the my_module object in such a way, that methods present in my_module1 will be accessed like my_module.my_module1.Method1().

//method to extend the object
var extend = function(target, source, fileName) { //added fileName as new parameter
	target[fileName] = {}; //creating a subclass for each file
	for (var i in source) {
		if (source.hasOwnProperty(i)) {
			target[fileName][i] = source[i]; //adding all methods in fileName class
		}
	}
}

my_module.instance = null;
my_module.getInstance = function(){
    if(my_module.instance === null){
        my_module.instance = new my_module();

		//list of files we have created
    	var files = [
			'my_module1',
			'my_module2'
		];
		
		var fs = require("fs");

		files.forEach(function(fileName){
			//you can pass any dependencies to sub files here
	        var fileObj = new (require('./' + fileName))(my_module.instance);
	        //adding fileObj object properties to the my_module
	        extend(my_module.instance, fileObj, fileName);
		});
    }
    return my_module.instance;
}


Now to access method1 in my_module2 we would have to use parent.my_module1.Method1().
var my_module2 = function(parent){ //parent is reference of my_module object
	this.Method2 = function(){
		return parent.my_module1.Method1() + " " + "world"; //calling method1 using parent object, instead of "this"
	}
}	
module.exports = my_module2;
Add Comments :

Login

Register