Skip to content

Creating custom modules

Nishant Mendiratta edited this page Mar 16, 2014 · 1 revision

Create a module and use it to our programs. Module are LIKE FileSystem/HTTP/HTTPS

Creating a file my_module.js

    function hello(){
         return "world";
    }

    function helloAgain(){
         return hello() + " again";
    }

    function myPrivateFunction(number){
         return number + 1;
    }

    function increment(number){
         return myPrivateFunction(number);
    }

//Explicitly declaring objects to be used publicly

   module.exports.helloworld = hello;
   module.exports.helloworldagain = helloAgain;
   module.exports.privatefunctionincrement = increment;

Creating a file server_accessing_my_module.js

/Accessing function/

  var myModule = require("./my_module.js");
  console.log("First module message : " + myModule.helloworld());
  console.log("Second module message : " + myModule.helloworldagain());
  console.log("Third module message : " + myModule.privatefunctionincrement(1));

  var git_module = require("./github_module.js");
  git_module.github("nishantmendiratta",function(repos){
         console.log("Nishant Mendiratta repos",repos);
  });

References

Youtube Reference