Spawn process on remote host in NodeJS
<p>I have a simple Node server that serves a page with a button. On click that button needs to execute a command on a remote host. When the command finishes, the output should then be given to the response of the function and shown in the html. </p>

<p>Currently this is the server code:</p>

<pre><code>app.use(logger("combined"));
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());

app.get('/index.html', function (req, res) {
    res.sendFile( __dirname + "/" + "index.html" );
})

app.post('/connect', function (req, res) {
    var spawn = require('cross-spawn-async');
    var mpiProc = spawn('ssh','pi@raspi2','"ls"', { stdio: ['pipe','pipe',2,'pipe'] });

    var grid = "...8.1..........435............7.8........1...2..3....6......75..34........2..6..";
    str = "";
    mpiProc.stdout.on('data', function(data) {
        console.log(data);
        str = data.toString();
        res.write('data: ' + JSON.stringify({ msg : str }) + '\n\n');
        // Output       
    });

    mpiProc.on('close', function(code) {
        res.end(str);
        // Script exit code
    });

    mpiProc.on('error', function(code) {
        res.end('stderr: ' + code);
        // Script exit code
    });
}) 
    require('http').createServer(app).listen(3000, function(){
    console.log('Listening on 3000');
});
</code></pre>

<p>As you can see the following call tries to execute the 'ls' command on a remote host:</p>

<pre><code>var mpiProc = spawn('ssh','pi@raspi2','"ls"', { stdio: ['pipe','pipe',2,'pipe'] });
</code></pre>

<p>But this doesn't return anything, while executed from the server directly from terminal, it returns the list of directory content. To test if the command works this way with a spawn I tried doing this:</p>

<pre><code>var mpiProc = spawn('ls', { stdio: ['pipe','pipe',2,'pipe'] });
</code></pre>

<p>Which returns what I expected, the list of directory content on the server.
So how can I create a pipe to a remote host to execute a command and then store the result of the command on the server?</p>