【发布时间】:2012-03-15 22:28:39
【问题描述】:
我可以使用以下代码从 Twitter 获取 JSON 流到客户端:
var command = 'curl -d @tracking https://stream.twitter.com/1/statuses/filter.json -uUsername:Password'
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
child = exec(command);
child.stdout.on('data', function(data) {
res.write(data);
});
}).listen(1337, "127.0.0.1");
但我无法从 JSON 中获取 'text' 或 'id' 值。我尝试过使用 jQuery 的 parsJSON() 和其他东西,例如这样的代码:
var command = 'curl -d @tracking https://stream.twitter.com/1/statuses/filter.json -uUsername:password'
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
child = exec(command);
child.stdout.on('data', function(data) {
for (i=0; i<data.length; i++) {
reduceJSON = data[i]["text"]
stringJSON = String(reduceJSON)
res.write(stringJSON);
}
});
}).listen(1337, "127.0.0.1");
我不断收到“未定义”或“readyStatesetRequestHeadergetAllResponseHeadersgetResponseHeader”或“object:object”的流。有人知道如何获得个人价值吗?
【问题讨论】:
-
你为什么使用 CURL 和 exec 而不是节点请求库?