I have one way communication between two express servers. The slave server is using axios to make a get req to the master server, which replies with an object. This object includes a function. I can see the function inside the object on the master server, but when I log the axios response on the slave server the function is no longer in the object.
I believe res.send and res.json are never sending the included function. Is this intended? and/or are there any workarounds? The end goal is to run this function on the slave server using other data included in the same object
Master Server
const testObj = {
data: {
val1: "string",
val2: "string"
},
makeString: function(values) { // I know this function doesn't make sense, but it's close enough for the purpose of example
return values.data.val1 + values.data.val2
}
}
app.get('/', (req, res) => {
console.log('sent object, testObj') // function can be seen included in the object here
res.send(testObj)
})
Slave Server
axios.get(`http://localhost:3000`, {})
.then(response => {
console.log(response.data) // function is no longer included in the object here
}
);
@eschwelgin I think res.send/res.json does a JSON.stringify() somewhere in the code. functions are ignored while serializing the JS object. You may try something like a .toString() on the function if you want to return the function back as a string. Something like this
const testObj = {
data: {
val1: "string",
val2: "string"
},
makeString: (function(values) {
return values.data.val1 + values.data.val2
}).toString()
}
@eschwelgin Solution with passing function in response and executing it on other side looks like a code smell for me. It鈥檚 definitely better to move this function in a separate API endpoint on master.
In any case this is not looks like an express issue, please let me know if this can be closed.
@harshit-sinha-developer Thank you for the insight, a quick test of your solution seems to work.
@rodion-arr I realize it may look that way, and I appreciate your insight. Seeing as how harshit-sinha's solution worked I think the issue lies in stringify and not express. Looks solved to me, thank you
@eschwelgin You are welcome. But I guess it's not an issue in stringify (it's not a bug it's a feature 馃槈 )
JSON is designed for data and not methods. But I am glad the solution might work for you in some special case
@harshit-sinha-developer You're right I wouldn't really call it an 'issue' at this point, as I'm probably outside of the intended scope of these packages. I'm going to have to spend some time working on a way to convert a string => function. If you have any insight there feel free to share, but honestly I haven't even done a through search yet. TY again brother
Most helpful comment
@harshit-sinha-developer Thank you for the insight, a quick test of your solution seems to work.
@rodion-arr I realize it may look that way, and I appreciate your insight. Seeing as how harshit-sinha's solution worked I think the issue lies in stringify and not express. Looks solved to me, thank you