Search code examples
node.jsmoduleamdnode-modules

Node modules usage error


Ive node moudle which i need to export two functions and get to both of the function a parameter arg1,I try with the following I got error,what am I doing wrong here ?

UPDATE

Ive two method inside module

1. I need to expose it outside and to call it explicit from other
 module with parameter 

like

require('./controller/module')(functionName1)(parameter);
  1. another function(functionName2) in this module which I need to call it explicit with two parameter ,how should I do it right?

Solution

  • It is not very clear what you want to do, but i think you want something like that:

    module.exports = function (arg1)  {
      return {
        server: function (params1) {
          //do something with arg1 and params1
        },
        proc: function (params2) {
         //do something with arg1 and params2
       }
     }
    };
    

    And using the module:

    var arg1 = 'whatever'
    var myMod = require('myMod')(arg1);
    myMod.server();
    myMod.proc();
    

    Option 2

    If i look at your new example

    require('./controller/module')(functionName1)(parameter);
    

    you need module that exports a function that returns another function (Higher Order Function).

    So for example:

    module.exports = function(functionName1) {
        if(functionName1 === 'server'){
            return function server(parameter){
              //do your stuff here
           }
        }
    
        if(functionName1 === 'proc'){
            return function proc(parameter){
              //do your stuff here
           }
        }    
    };