我需要使用 node.js 在 C++ 中运行编译文件,并从该文件中返回一个值。 我尝试使用 child_process.execFile,但我没有遇到任何问题。 这是我使用的功能:
var exec = require('child_process');
Test.prototype.write = function (m) {
var wRet;
exec.execFile ('./mainCmd', ['-o', '\\!' + m + '.']
function (error, stdout, stderr) {
wRet = stdout;
console.log ("wRet" + wRet);
return wRet;
});
}
问题是“console.log”中的 wRet 包含从文件 c++ 返回的文本,在“return”中仍未定义。
你能帮忙吗?
谢谢大家!
最佳答案
您必须将回调传递给您的 test()
函数:
var chproc = require('child_process');
Test.prototype.write = function(m, cb) {
chproc.execFile(
'./mainCmd',
['-o', '\\!' + m + '.'],
function(error, stdout, stderr) {
if (error) return cb(error);
cb(null, stdout);
}
);
};
// usage ...
var t = new Test();
t.write('foo', function(err, result) {
if (err) throw err;
// use `result`
});
关于javascript - Node js child_process.exec文件返回,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23590501/