2015-09-28 48 views
0

我想平的结果(isAlive)添加到流以下,这样以后我能管的HTTP响应,但我收到以下错误的NodeJS创建自定义流

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); 
+0

您正在使用哪个版本的节点? – Chance

+0

我使用的是v0.10.40 – irom

回答

1

我修改你的代码:

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监听到你的读者,你会看到你的工具'正在使用抛出以下错误:Error: not implemented

对不起,我以为我之前发过这个。幸运的是,堆栈保存未提交的帖子。