Transfer process output to HTTP response in nodejs

  • 2021-08-05 08:13:53
  • OfStack

No more nonsense, just post the code for everyone. The specific code is as follows:


var spawn = require('child_process').spawn;
require('http').createServer(function(req, res) {
 var child = spawn('tail', ['-f', '/var/log/system.log']);// When there is 1 When a new request appears, it is executed by executing  tail -f /var/log/system.log Command start 1 A new process 
 child.stdout.pipe(res);// Passing the output of the child process into the responding body 
 res.on('end', function() {
 child.kill();
 });
}).listen(4000);// Stream service that can generate processes and transmit process output, and end sub-processes as needed 

The above is the site to introduce the nodejs to HTTP response transmission process output, I hope to help you, if you have any questions welcome to leave me a message, this site will reply to you in time!


Related articles: