requirement will took you to invent. so if you know what your requirement then you will try to invent it.
a good programmer invent or write/hack a code one time but use again and again so today i will tell you how you create/hack a module one time for future requirement in node.js.
create a module in node.js is so easy no complication is here .
ok lets hack first of you need to create a folder "node_modules" if not exist.
in node have two keyword for package manage one "require" and another "exports" require for import a package from node module and another for export a module to app.
create a file like "new_module.js" file or anything and write your function in this file only follow bellow structure:
exports.function_name=function(){
//your code here
};
it's so easy you can see what i have create:
exports.add=function(a,b){
return a+b;
};
exports.sub=function(a,b){
if(a>b){
c=a-b;
}
else{
c=b-a;
}
return c;
};
exports.div=function(a,b){
if(a>b){
c=a/b;
}
else{
c=b/a;
}
return c;
}
exports.mul=function(a,b){
return a*b;
}
it's just a mathematical operation module nothing else.
ok now your module is ready for import.
oho yea if your function is require other package then create a folder in node_module, suppose new_module and create a new folder node_module here or open terminal and go to new_module folder and install your new module:
~/node/node_module/new_module$ sudo npm <module name>
it's so simple ok now you have install your require module now you can import this module from your module.
now create a file "index.js" and write your function code. you have create a new module.
now time to import your module in your application so now go to application folder and import your module by bellow structure.
var new_mod=require(module_name);
now you can call all function by new_mod object.
how i call my module you can see bellow:
var new_module=require('new_module');
console.log(new_module.add(5,4));
console.log(new_module.sub(5,7));
console.log(new_module.sub(7,4));
console.log(new_module.div(5,11));
console.log(new_module.div(8,4));
console.log(new_module.mul(7,4));
oho it's nice and simple.
ok now try it and write/hack one time use again and again
enjoy
a good programmer invent or write/hack a code one time but use again and again so today i will tell you how you create/hack a module one time for future requirement in node.js.
create a module in node.js is so easy no complication is here .
ok lets hack first of you need to create a folder "node_modules" if not exist.
in node have two keyword for package manage one "require" and another "exports" require for import a package from node module and another for export a module to app.
create a file like "new_module.js" file or anything and write your function in this file only follow bellow structure:
exports.function_name=function(){
//your code here
};
it's so easy you can see what i have create:
exports.add=function(a,b){
return a+b;
};
exports.sub=function(a,b){
if(a>b){
c=a-b;
}
else{
c=b-a;
}
return c;
};
exports.div=function(a,b){
if(a>b){
c=a/b;
}
else{
c=b/a;
}
return c;
}
exports.mul=function(a,b){
return a*b;
}
it's just a mathematical operation module nothing else.
ok now your module is ready for import.
oho yea if your function is require other package then create a folder in node_module, suppose new_module and create a new folder node_module here or open terminal and go to new_module folder and install your new module:
~/node/node_module/new_module$ sudo npm <module name>
it's so simple ok now you have install your require module now you can import this module from your module.
now create a file "index.js" and write your function code. you have create a new module.
now time to import your module in your application so now go to application folder and import your module by bellow structure.
var new_mod=require(module_name);
now you can call all function by new_mod object.
how i call my module you can see bellow:
var new_module=require('new_module');
console.log(new_module.add(5,4));
console.log(new_module.sub(5,7));
console.log(new_module.sub(7,4));
console.log(new_module.div(5,11));
console.log(new_module.div(8,4));
console.log(new_module.mul(7,4));
oho it's nice and simple.
ok now try it and write/hack one time use again and again
enjoy