0

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

6
  • functions have a toString() method that returns the "source code" of the function declaration. Commented May 29, 2017 at 11:39
  • Ok so what's next? I was thinking of splitting the code on "()" and reforming it using a function builder or something. Whhat's ur suggestion? Commented May 29, 2017 at 11:42
  • 2
    Yeah I think that is the way to go. You could do string manipulation/regex to strip out just the body of the function and use a 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. Commented May 29, 2017 at 11:48
  • On the surface, this sounds like a terrible idea, either opening your app up to hackers or else you're trying to do something nefarious. Can you explain better your goal? There may be a much better approach than the specific mechanism you're asking for. Commented May 29, 2017 at 11:52
  • I was thinking the same thing regarding the Function() builder I just that there should be a better way o. Commented May 29, 2017 at 11:52

1 Answer 1

1

It's strongly discouraged the use of eval in that way. This opens a lot of serious security issues.

You have few ways:

1) Store your functions in node.js inside an object, and use the name of the function as key. Then in your callback just pass the name of the function and retrieve the corresponding function and execute it

2) Provide a parameter and create a switch statement when depending on the parameter you execute the desired function

3) If you define your functions in your node.js file, everything that is defined is stored in the file's scope. So actually you already have your functions defined there and they can be accessible through brackets notation from the current scope too.

I hope it helps

Edited for showing an example of the first method:

let myFunctions = {};
myFunctions['f'] = function (){
  return "Returned Value Here";
};
myFunctions['bar'] = function (){
  console.log("Test");
};
axios({url: '/', method: 'POST', data: {something: "foo", bar: f}})
.then((response)=>{
 // considering in your response you have the bar function to be executed you can just do
 myFunctions['bar']();
.catch((reason){console.log(reason)})
Sign up to request clarification or add additional context in comments.

1 Comment

Could you please provide an example for the first method you mentioned?

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.