I would like to pass a javascript function in Node.js as a parameter in a URL from one server to another. I am using axios npm package to send the data, here's a sample of the code:
let f = function(){
return "Returned Value Here";
}
axios({url: '/', method: 'POST', data: {something: "foo", bar: f}})
.then((response)=>{console.log(response)})
.catch((reason){console.log(reason)})
Acccording to other questions it was suggested that I use eval() javascript function to evaluate the code but this causes a problem.
Firstly I would like to pass the function on the receiver side and NOT evaluate it. And even if I evaluate to get a function it would be a named function (I want to pass an anonymous function) that canNOT be referenced
For example
let bar = eval('function baz(){console.log("Test");}')
bar is not a reference of the function (bar is undefined) and also if you use typeof baz() the returned value is STRING.
So how can I pass a function in a URL?
Keep in mind POST requests send data as binary
toString()method that returns the "source code" of the function declaration.Function()constructor to recreate the function. Passing arguments around should be a bit trickier. IMO however, it does not sound great to be passing "behavior" to-from servers. You design servers to have a well defined behavior and pass only data back-and-forth.