2015-09-28 30 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

對不起,我以爲我之前發過這個。幸運的是,堆棧保存未提交的帖子。