我有以下简单的 Coffeescript 代码来访问 Twitter Stream API
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 EventEmitters 中的 error
事件如果没有监听,将会抛出。您需要添加
res.on 'error', # whatever
并处理错误。
请注意,这只会阻止抛出错误;它不会阻止错误的发生。
关于node.js - Node 套接字在 Twitter 流 filter.json 上挂起,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12434118/