Spartan Troy Spartan Troy - 2 months ago 10
Node.js Question

how to call methods with variable in node.js

how to call methods with variable in node.js
this is so that ;

var file=require("./file");
var method=b;

file.method();


//output
//error : TypeError: file.method is not a function

how to use it?

Answer

ok try this then

//file.js
module.exports = { 
    index: function () { 
        console.log("index called");
    },
    index2 :function() { 
        console.log("index2 called");
    } ,
    index3 : function () { 
        console.log("index3 called");
    } 
};

then

app.get("file/:method",function (req,res)
{
    var method = req.params.name;

    var file = require('./file');

    file[method]();
}
Comments