I have a node js server, which triggers a shell script that I have. The code is as follows (simple Hello World example):
var http = require("http");
function onRequest(request, response) {
console.log("Request received.");
response.writeHead(200, {"Content-Type": "text/plain"});
var spawn = require('child_process').spawn;
//executes my shell script - main.sh when a request is posted to the server
var deploySh = spawn('sh', [ 'main.sh' ]);
//This lines enables the script to output to the terminal
deploySh.stdout.pipe(process.stdout);
//Simple response to user whenever localhost:8888 is accessed
response.write("Hello World");
response.end();
}
http.createServer(onRequest).listen(8888);
console.log("Server has started.");
The code works perfectly - A server is started and whenever the page is loaded (request) in the browser, the shell script runs on the server and outputs its results on the terminal.
Now, I want to display that result back to the client browser instead of just "Hello World". How to do that? Please note that the script takes 4 - 5 seconds to execute and generate a result.