0

I write a Node.js application that get from another app (server) information about instance of some object of the server. The information about object presented as tree with information about node type (method or property, method args, node id, etc).

Is it possible in Node.js to generate some object (like JSON but with functions) at runtime that is a representation of the server object instance to call its methods directly?

Or basically: is it possible to generate object with callable method at runtime in Node.js?

2
  • I'm afraid it's not clear what you want to do here. Of course you can create objects with functions at runtime, it's a common thing to do. Are you trying to send those objects somewhere? For instance, to some client? What kind of client? A web browser? Something else? If not a browser, how does the client communicate with the server? Commented Apr 8, 2019 at 7:30
  • It's is next environment: C++ server that provide call-points for his COM objects. Node.js app is client of this server. At the same time it's backend to Angular frontend. My goal is create call-points for frontend application. According to my idea I do it by generating intermediate objects on backend and synchronize this object between frontend and backend Commented Apr 8, 2019 at 7:46

1 Answer 1

1

This is possible. But you should worry about security concerns about this I guess. All security concerns aside this is how to do this.

const func = 'function(x) {console.log(x);}';

const callable = eval(func);
callable('Hello World');

So you receive the function as a string and you can conver it to a callable function by executing as code using eval which is concerned to be evil in any programming language. So basically the one that send you the code can do anything to your server, if that is a concern this should be avoided.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.