node.js - Dynamically load Javascript functions -


I want to load a dynamically and want to call Javascript functions with a JSON file. This idea is to build a plugin framework that will enable others to add functionality by typing a function and updating the JSON file. For example, with the following JSON:

  {"plugins": {"random": {"name": "random number generator", "hook": "random"}}}   

... and the following plugin: random.js

  module.export.ron = function () {return mathematics. (Mathematics. RANDOM () * 100); }   

I want to be able to parse the JSON file and want to call the run function of any plugin How can I load and call the run function on random?

Depending on what you have described, just need the requirement module.

  var plugins = required ('./plins.json'); Var plugins for = objects Kids (plugins); For (var i = 0; i & lt; plugin kios; i ++) plugins [plugin keys [i]]. Func = is required ('.' '[Plugin ki [i]. Hook +' .js'). Run; // You can also add additional paths in addition to the above. You can also leave '.js'   

Then you simply:

  var randomTheHardWay = plugins.random.func ();    

Comments

Popular posts from this blog

Pass DB Connection parameters to a Kettle a.k.a PDI table Input step dynamically from Excel -

multithreading - PhantomJS-Node in a for Loop -

c++ - MATLAB .m file to .mex file using Matlab Compiler -