I need to call a python function from a NodeJS service code. I checked this link and wrote below nodeJS code
const express = require('express')
const app = express()
let runPy = new Promise(function(success, nosuccess) {
const { spawn } = require('child_process');
const pyprog = spawn('python', ['./ml.py']);
pyprog.stdout.on('data', function(data) {
success(data);
});
pyprog.stderr.on('data', (data) => {
nosuccess(data);
});
});
app.get('/', (req, res) => {
res.write('welcome\n');
runPy.then(function(testMLFunction) {
console.log(testMLFunction.toString());
res.end(testMLFunction);
});
})
app.listen(4000, () => console.log('Application listening on port 4000!'))
Suppose I have a sample python code in ml.py
like below
def testMLFunction():
return "hello from Python"
Now when I run the nodeJS code and do a GET through Curl, I only see the message 'welcome'
which is the console log of GET endpoint. But I don't see the message that is returned from the python function anywhere. What am I missing?