简体   繁体   English

如何使用 res.write 从 Node js 发送多个响应,以便数据实时显示在客户端中?

[英]How to send multiple responses from Node js using res.write, so that the data appears live in the client?

I want to send data immediately to the client from res.write, but the data is being sent exactly once in one response.我想立即从 res.write 向客户端发送数据,但数据在一个响应中只发送一次。 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();
  });

    });

I think you just add another line res.write(): - Example:我认为您只需添加另一行 res.write(): - 示例:

ls.stdout.on('data', function (data) {
  console.log('stdout: ' + data.toString());
  res.write(data.toString());
  response.write("foo");
  response.write("bar");
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM