I want to send data immediately to the client from res.write, but the data is being sent exactly once in one response. How can i send multiple responses so the data is sent live to the client?
app.get('/api/simulator', (req,res) => {
res.setHeader('Content-Type','text/html');
var spawn = require('child_process').spawn,
ls = spawn('../abc.sh');
ls.stdout.on('data', function (data) {
console.log('stdout: ' + data.toString());
res.write(data.toString());
});
ls.stderr.on('data', function (data) {
console.log('stderr: ' + data.toString());
});
ls.on('exit', function (code) {
console.log('child process exited with code ' + code.toString());
res.end();
});
});
res.write(), the client in the browser typically won't show the data until it has all arrived. If you can be more specific about exactly what you're trying to do, perhaps we could help in more detail. A webSocket connection would allow you to send individual chunks of data that would be received by the client immediately.