node.js调用外部exe并等待输出 [英] node.js call external exe and wait for output

查看:328
本文介绍了node.js调用外部exe并等待输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只想从nodejs-App调用外部exe。这个外部exe进行一些计算并返回nodejs-App所需的输出。但我不知道如何在nodejs和外部exe之间建立连接。所以我的问题是:

I just want to call an external exe from a nodejs-App. This external exe makes some calculations and returns an output the nodejs-App needs. But I have no idea how to make the connection between nodejs and an external exe. So my questions:


  1. 如何从nodejs中正确调用具有特定参数的外部exe文件?

  2. 我如何有效地将exe的输出传输到nodejs?

Nodejs应等待输出外部的exe。但是nodejs如何知道exe何时完成处理?然后我如何提供exe的结果?我不想创建一个临时文本文件,我将输出写入,而nodejs只是读取此文本文件。有什么办法可以直接将exe的输出返回给nodejs吗?我不知道外部exe如何直接将其输出传递给nodejs。 BTW:exe是我自己的程序。因此,我可以完全访问该应用程序,并可以进行任何必要的更改。欢迎任何帮助...

Nodejs shall wait for the output of the external exe. But how does nodejs know when the exe has finished its processing? And then how do I have to deliver the result of the exe? I don't want to create a temporary text-file where I write the output to and nodejs simply reads this text-file. Is there any way I can directly return the output of the exe to nodejs? I don't know how an external exe can directly deliver its output to nodejs. BTW: The exe is my own program. So I have full access to that app and can make any necessary changes. Any help is welcome...

推荐答案


  1. 使用 child_process 模块。

  2. 使用stdout。

  1. With child_process module.
  2. With stdout.

代码将如下所示

var exec = require('child_process').exec;

var result = '';

var child = exec('ping google.com');

child.stdout.on('data', function(data) {
    result += data;
});

child.on('close', function() {
    console.log('done');
    console.log(result);
});

这篇关于node.js调用外部exe并等待输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆