nodejs 启动第三方exe

  1. nodejs启动第三方的exe、传入启动参数,并设置工作路径,代码如下
const iconv = require('iconv-lite'); # 解决乱码问题
const { spawn } = require('child_process');

function say() {
  const exePath = 'D:/helle.exe';	// exe绝对路径
  const args = ['argc1', 'argc2', 'argc3']; // exe启动参数
  const options = {
    cwd: 'D:/work'					// 设置exe工作路径
  };
  const childProcess = spawn(exePath, args, options);
  // 监听子进程的输出
  childProcess.stdout.on('data', (data) => {
    console.log(`stdout: ${iconv.decode(data, 'gbk')}`);
  });

  childProcess.stderr.on('data', (data) => {
    console.error(`stderr: ${iconv.decode(data, 'gbk')}`);
  });

  // 监听子进程的退出
  childProcess.on('close', (code) => {
    console.log(`子进程退出,退出码 ${code}`);
  });
}

function execute(someFunction) {
  someFunction();
}

execute(say);

你可能感兴趣的:(开发语言)