在 node.js 中生成一个子进程

spawn a child process in node.js

本文关键字:一个 子进程 node js      更新时间:2023-09-26

node.js中,我试图生成一个子进程

我必须在执行EXE文件时传递一个参数(mode=All

我通过以下方式做。但什么也得不到

`var exec = require('child_process').execFile;
var fun =function(){ 
   exec('Sample.exe mode=All', function(err, data) {  
        console.log(err)       
        console.log(data.toString());                       
    });  
}
fun();`

在命令行中,我得到的输出为

 `c:'files'Sample.exe mode=All`

输出如下

{"ID":"VM-WIN7-64","OS":"Windows 7"}{"ID":"VM-WIN7-32","OS":"Windows 7"}{"ID":"V M-WIN7-32-1","OS":"Windows 7"}{"ID":"VM-WIN7-32-2","OS":"Windows 8"}

如何使用节点获取上述输出.js

以下是文档中execFile函数签名:

child_process.execFile(file, args, options, callback)

您将可执行文件路径与空格和参数组合在一起。execFile并不期望这一点。根据文档尝试:

exec('Sample.exe', ['mode=ALL'], {}, function(err, data) {