0

I want to get the result back from a execFile call:

var http = require('http');
var fs = require('fs');
const execFile = require('child_process').execFile;

http.createServer(function (req, res) {
  fs.readFile('index.html', function(err, data) {
    res.writeHead(200, {'Content-Type': 'text/html'});
    res.write(execute());
    return res.end();
  });
}).listen(8080);


var execute = function(){
  console.log("start execution");
    let result = execFile('./demo-files/demo.sh', ['1st', '2nd', '3rd'], function(err, data){
    if(err){
      console.log("ERROR:\n" + err);
    }
    return data.toString();
  });
  return result;
}

But I'm not sure which way is the right syntax.

Surprisingly, I can't seem to find the exact post on SO with my question? If duplicate, I will happily consider the other source.

Alex Coleman
  • 607
  • 1
  • 4
  • 11
  • The general solution to returning asynchronously retrieved data is [here](https://stackoverflow.com/a/14220323/816620). In a nutshell, your choices are to use a callback, a promise or an event. You can't return the value directly unless you switch to `execFileSync()` which would not be advised for a server. – jfriend00 Nov 20 '20 at 22:50
  • Try to execute sychronous command. https://stackoverflow.com/questions/4443597/node-js-execute-system-command-synchronously – Filipe Mendes Nov 20 '20 at 22:55

0 Answers0