我需要从NodeJS服务代码调用python函数。我检查了这个链接并写下了nodeJS代码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!'))
假设我有一个示例python代码,ml.py如下所示def testMLFunction():
return "hello from Python"
现在,当我运行nodeJS代码并通过Curl执行GET时,我只看到消息'welcome',即GET端点的控制台日志。但我没有看到python函数返回的消息。我错过了什么?