gpt4 book ai didi

node.js - 使用 child_process 将 'ping' 输出显示到浏览器

转载 作者:太空宇宙 更新时间:2023-11-04 00:56:16 24 4
gpt4 key购买 nike

如何将 ping 的输出异步流式传输到浏览器,child_process.spawn() 是一个异步调用。但是如何将进程传输到 browser/html 呢?有什么建议吗?

这是代码

var spawn = require('child_process').spawn;
var ping = spawn('ping',['www.google.com'],
{ stdio: ['pipe', process.stdout, process.stderr] });

最佳答案

如果您使用Express , res 是可靠的

var spawn = require('child_process').spawn;
app.get('/', function(req, res, next) {
var ping = spawn('ping', ['www.google.com']);
ping.stdout.pipe(res);
});

顺便说一句,有一个 ping模块也是如此。

关于node.js - 使用 child_process 将 'ping' 输出显示到浏览器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29673608/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com