在 nodejs 中创建自定义流
Creating custom stream in nodejs
我正在尝试将 ping 结果 (isAlive
) 添加到下面的流中,以便稍后我可以通过管道传输到 http 响应,但我收到了以下错误
events.js:85
throw er; // Unhandled 'error' event
我不明白这是什么意思,如果能为我提供任何帮助,我将不胜感激?
var Readable = require('stream').Readable;
var s = new Readable;
var ping = require('ping');
var hosts = ['192.168.1.1', 'google.com', 'yahoo.com'];
hosts.forEach(function(host){
ping.sys.probe(host, function(isAlive){
var msg = isAlive ? 'host ' + host + ' is alive' : 'host ' + host + ' is dead';
//console.log(msg);
s.push(msg)
});
});
//s.push(null) ;
//s.pipe(process.stdout);
我修改了你的代码:
var Readable = require('stream').Readable;
var s = new Readable();
var ping = require('ping');
s.on('error', function(err){
console.error(err)
})
var hosts = ['192.168.1.1', 'google.com', 'yahoo.com'];
hosts.forEach(function(host){
ping.sys.probe(host, function(isAlive){
var msg = isAlive ? 'host ' + host + ' is alive' : 'host ' + host + ' is dead';
//console.log(msg);
s.push(msg);
})
});
您在 var s = new Readable
()
上错过了 ()
通过将 error
侦听器添加到您的 reader,您将看到您使用的工具抛出以下错误:Error: not implemented
抱歉,我想我之前已经发送过了。幸运的是堆栈保存未提交的帖子。
我正在尝试将 ping 结果 (isAlive
) 添加到下面的流中,以便稍后我可以通过管道传输到 http 响应,但我收到了以下错误
events.js:85
throw er; // Unhandled 'error' event
我不明白这是什么意思,如果能为我提供任何帮助,我将不胜感激?
var Readable = require('stream').Readable;
var s = new Readable;
var ping = require('ping');
var hosts = ['192.168.1.1', 'google.com', 'yahoo.com'];
hosts.forEach(function(host){
ping.sys.probe(host, function(isAlive){
var msg = isAlive ? 'host ' + host + ' is alive' : 'host ' + host + ' is dead';
//console.log(msg);
s.push(msg)
});
});
//s.push(null) ;
//s.pipe(process.stdout);
我修改了你的代码:
var Readable = require('stream').Readable;
var s = new Readable();
var ping = require('ping');
s.on('error', function(err){
console.error(err)
})
var hosts = ['192.168.1.1', 'google.com', 'yahoo.com'];
hosts.forEach(function(host){
ping.sys.probe(host, function(isAlive){
var msg = isAlive ? 'host ' + host + ' is alive' : 'host ' + host + ' is dead';
//console.log(msg);
s.push(msg);
})
});
您在 var s = new Readable
()
()
通过将 error
侦听器添加到您的 reader,您将看到您使用的工具抛出以下错误:Error: not implemented
抱歉,我想我之前已经发送过了。幸运的是堆栈保存未提交的帖子。