节点套接字挂在Twitterstreamfilter.json get

我有以下简单的Coffeescript代码来访问Twitter streamAPI

http = require 'http' options = port: 443 host: 'stream.twitter.com' path: '/1.1/statuses/filter.json?track=keyword' headers: 'Authorization': 'Basic ' + new Buffer('[user]:[pass]').toString 'base64' 'Host': 'stream.twitter.com' method: 'GET' callback = (res) -> console.log res res.on 'data', (chunk) -> console.log chunk res.on 'end', -> console.log 'end' # res.on 'close', -> res.emit 'end' http.request options, callback 

为什么我得到

 throw arguments[1]; // Unhandled 'error' event Error: socket hang up 

一个错误是触发你的响应( res ),你没有听取error事件。 默认情况下,Node.js EventEmitter中的error事件,如果没有监听,将抛出。 你需要添加

 res.on 'error', # whatever 

并处理错误。

请注意,这将保持错误被抛出; 它不会停止发生错误。