request.on(“response”,[…]) 永远不会被解雇
我目前正在摆弄 NodeJS 并尝试使用 Twitter-Search API。使用curl
它工作得很好 - 所以我的防火墙或其他什么都没有问题。然而,我从未在 NodeJS 中得到回应。
var sys = require("sys"),
http = require("http"),
events = require("events");
sys.puts("Hi there… ");
var client = http.createClient(80, "search.twitter.com"),
body = "",
query = "foobar";
function getResults() {
sys.puts("fetching for "+query);
var request = client.request("GET", "/search.json?q="+query);
request.on("response", function(data){
/* this somehow never gets fired :( */
sys.puts("BODY:"+ data);
});
}
var interval = setInterval(getResults, 5000);
欢迎任何提示或解决方案!
提前致谢。
I'm currently messing around with NodeJS and try to use the Twitter-Search API. With curl
it works fine - so there's nothing wrong with my firewall or whatever. Yet, I never get a response within NodeJS.
var sys = require("sys"),
http = require("http"),
events = require("events");
sys.puts("Hi there… ");
var client = http.createClient(80, "search.twitter.com"),
body = "",
query = "foobar";
function getResults() {
sys.puts("fetching for "+query);
var request = client.request("GET", "/search.json?q="+query);
request.on("response", function(data){
/* this somehow never gets fired :( */
sys.puts("BODY:"+ data);
});
}
var interval = setInterval(getResults, 5000);
And the URL is also working.
Any hints or solutions are welcome!
Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您从不发送请求。
您需要使用
request.end()
此外,
response
事件的参数是response 对象
不是身体。您需要在响应对象上设置data
事件,然后监听end
事件以确保您获得了所有数据。请参阅: http://nodejs.org/api/http.html#http_class_http_clientrequest
更多提示
querystring.escape
对搜索参数进行 urlencodeHost
标头,否则 Twitter 将返回404固定代码:
You never send the request.
You need to use
request.end()
Also the
response
event'S parameter is theresponse object
NOT the body. You need to set thedata
event on theresponse object
and then listen for theend
event to make sure you got all the data.See: http://nodejs.org/api/http.html#http_class_http_clientrequest
A few more tips
querystring.escape
to urlencode your search parameterHost
header, otherwise Twitter will return a 404Fixed code: