David Dias
David Dias

Reputation: 1832

Best way to transmit entire Javascript Objects over a REST API in Node.js

I'm trying to figure out how to transmit a JS (not JSON) over a REST API, basically the idea is that the client can send the object with functions () that will provide the functionality for posterior execution.

For example How to transmit

var jsObj = {
 a: 1
 b: function () { console.log("B") }
}

from Node A to Node B, so that now Node B knows how to execute b()

Thank you

Upvotes: 1

Views: 216

Answers (1)

tandrewnichols
tandrewnichols

Reputation: 3466

If you are in control of both services, why not just have Node A report a string API endpoint to Node B that Node B can hit with certain data? Like

var jsObj = {
    a: 1
    b: "http://api.somedomain.com/api/b/"
};

And then have Node B call that endpoint with whatever data is necessary (in this example with mikeal's request module):

request.get(jsObj.b + "helloworld");

And have a route in Node B that matches /api/b/:param and a function to handle it (not sure what you're using for routing - this is sort of an express specific example).

This is kind of the idea behind service oriented architecture: each service knows how to handle a subset of functionality and the services talk to each other to complete tasks.

Upvotes: 1

Related Questions