我只是想深入研究 Node.js,并且我正在测试我认为有助于完成我的项目的基本功能。其中之一是执行我用 C++ 编写的一个小的 .exe 文件,该文件处理来自文本文件的数据。我发现 child_process.execFile 可能是实现这一目标的最佳方法。所以我写了一个小脚本来启动一个服务器并调用一个可执行文件。对于第一个测试,我将路径设置为“第三方”-.exe,它工作得很好(程序按预期启动)。但是,当我以自己的 .exe 为目标时,什么都没有发生,尽管路径是正确的(我用 fs.access 进行了反核)。只需双击 .exe 手动执行文件也完全可以正常工作,并且相应地处理 txt 文件。所以现在我想知道是否得到某物。根本上是错误的,例如 .
这是我的代码:
var http = require('http');
const fs = require('fs');
var server = http.createServer(function(req, res){
console.log('Request was made: ' + req.url);
res.writeHead(200, {'Content-Type': 'text/plain'});
});
server.listen(3000, '127.0.0.1');
console.log('Listening to port 3000');
var executablePath = "C:/path/to/file.exe";
fs.access(executablePath, fs.constants.F_OK, (err) => {
console.log(`${executablePath} ${err ? 'does not
exist':'exists'}`);
});
const execFile = require('child_process').execFile;
const child = execFile(executablePath, (error, stdout, stderr) => {
if (error) {
console.error('stderr', stderr);
throw error;
}
console.log('stdout', stdout);
});
控制台输出是“...C:/path/to/file.exe 存在”。execFile 不会引发任何错误。感谢您的帮助,并为我的菜鸟语言道歉!