If I've got a function on my NodeJS server then I can expose it for use within a web-browser in just a few lines of code and then call it from the browser with $.getJSON(). I'll give an example of this below. If I want to standardise this sort of message-passing then there are plenty of protocols I could adhere to such as JSON-RPC 2.0, and APIs which would help me with that.
Now I've got about half a dozen such functions exposed, and it occurs to me it would be nice to bundle them all into a single object, pass that to the server for exposure all in one go, then have a proxy-object automatically constructed on the client-side matching the prototype of the server object and encapsulating the calls to $.getJSON(). (Obviously this would not be without its limitations - a synchronous function is not going to stay synchronous, and any error handling should probably be done in a standard way to fit in with errors generated by the message-passing protocol.)
Do you know of any software which would do something like this sort of JavaScript Object proxying for me? It seems like the sort of thing which might exist, though my Googling hasn't turned up anything. I could write it myself but it would take a long time to make a thorough job of it.
Of course if I hadn't written some reporting functionality on the client-side then decided that was really dumb and it belongs on the server, then I might never have noticed the clumsy difference in technique I currently require to access my core functionality from these two places ;-)
And here's the example I promised of how I'm doing things at the moment: If I've got a function on my NodeJS server...
function myFunction(arg1, arg2) {
return arg1 + arg2;
}
...then I can expose it for use within a web-browser in just a few lines of code...
var http = require('http');
var url = require('url');
http.createServer(function(request, response) {
var parsed = url.parse(request.url, true);
if (parsed.pathname == '/myFunction') {
var result = myFunction(parsed.query.arg1, parsed.query.arg2);
response.setHeader('Content-Type', 'application/json');
response.setHeader('Access-Control-Allow-Origin', 'null');
response.end(JSON.stringify(result));
}
}).listen(8080, 'localhost');
...and then call it from the browser with
$.getJSON('http://localhost:8080/myFunction?arg1=Hello&arg2=World', function(result) {
console.log(result);
});