javascript - How to call an external function in node js route -
i little confused on how works...
i writing node/express app , there function wrote in own file, need use in route , want call function.
in other file (tranformthedata.js) there is:
module.exports = { tranformthedata:function (data){ console.log('whatever') } in node app.js file have
var formatjson = require('./js').tranformthedata; can use
formatjson(data) and utilitze function? or have else, have seen few examples of doing not make sense me.
you should this:
// transformdata.js module.exports = { formatjson: function(data) { console.log('whatever') }, otherfunction: function() { } } // app.js var tranformthedata = require('./path/to/tranformthedata.js'); var formatjson = tranformthedata.formatjson; var otherfunction = tranformthedata.otherfunction; formatjson(data); // work module.exports in case exporting object literal, , object has 2 functions. requiring file , assigning variable assign variable object literal, has access methods.
when call methods, can pass in whatever params want. in case of formatjson, it's accepting data param.
Comments
Post a Comment