IntegerWolf IntegerWolf - 1 year ago 89
Javascript Question

Load js dynamically in an Node.js received from mongoDB (hosted @Azure)

I have a Node.js server hosted in Azure

Developers should be able to write JavaScript satisfying an API and upload it to my MongoDB hosted somewhere else.

My Node.js server now gets triggered and picks the right logic from the MongoDB.

  • How can I execute this Code dynamically the best way?

  • Use Eval? Or can I load the file in MainMemory and execute it?

The loaded code must be able to access other methods running on the Server (the ones documented in the API for the developer)

Answer Source

I found a pretty neat solution:

  • transform the JavaScript syntax to "Node.js" syntax
  • write the module with the fs-library to a file
  • "require" the created file to a variable
  • execute the logic of the variable
  • delete the file afterwards

--> you can now debug the code unlike with using "eval"

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download