Reputation: 65
I have to create an application in Node.js that executes algorithms (in js) and create graphs with their results.
My problem is that I don't really know how to execute the JavaScript in my textarea, I didn't find a plugin that can run a javascript code in node.js.
Thank you
Upvotes: 0
Views: 669
Reputation: 65
I have also found that https://nodejs.org/api/vm.html which is perfect !!
Upvotes: 0
Reputation: 650
You can use JSON.eval("some string of javascript") in most any JS dialect and it will run the code inside. See more info here :
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/eval
It's pretty obvious this is for school, but as a general practice using JSON.eval() is not recommended as it's a nice attack vector for running any code someone wants. However, it will work in node JS or on the client side in your browser wherever you insert it. As long as it's for a learning exercise it might be what you need, but don't go getting a job and using .eval() all over the place.
here's a copy paste of the warning from that page I linked to on developer.mozilla.org
Don't use eval needlessly! eval() is a dangerous function, which executes the code it's passed with the privileges of the caller. If you run eval() with a string that could be affected by a malicious party, you may end up running malicious code on the user's machine with the permissions of your webpage / extension. More importantly, third party code can see the scope in which eval() was invoked, which can lead to possible attacks in ways to which the similar Function is not susceptible.
Upvotes: 2
Reputation: 2308
You might be confusing a few concepts here. Node.js is server side javascript whereas your textarea in the browser is client side javascript.
To solve your current problem, you could either put the algorithm in the client or you could write some sort of REST service on the server side and call that service from the client.
Hope that helps
Upvotes: 1