javascriptnode.jsexpresscmd

node.js call external exe and wait for output


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. How do I call an external exe-file with specific arguments from within nodejs properly?
  2. And how do I have to transmit the output of the exe to nodejs efficiently?

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...


Solution

    1. With child_process module.
    2. With stdout.

    Code will look like this

    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);
    });