request.on(“response”,[…]) 永远不会被解雇

发布于 2024-10-06 11:55:54 字数 767 浏览 4 评论 0原文

我目前正在摆弄 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

如果没结果 2024-10-13 11:55:54

从不发送请求。

您需要使用request.end()

注意:请求不完整。该方法仅发送请求的标头。需要调用 request.end() 来完成请求并检索响应。 (这听起来很复杂,但它为用户提供了使用 request.write() 将正文传输到服务器的机会。)

此外,response 事件的参数是response 对象 不是身体。您需要在响应对象上设置data事件,然后监听end事件以确保您获得了所有数据。

request.on('response', function (response) {
  var body = '';
  response.on('data', function (chunk) {
    body += chunk;
  });
  response.on('end', function () {
    console.log('BODY: ' + body);
  });
});
request.end(); // start the request

请参阅: http://nodejs.org/api/http.html#http_class_http_clientrequest

更多提示

  1. 您可能需要使用 querystring.escape 对搜索参数进行 urlencode
  2. 您还需要设置 Host 标头,否则 Twitter 将返回404

固定代码:

var querystring = require('querystring');
...
var request = client.request("GET", "/search.json?q=" + querystring.escape(query), {'Host': 'search.twitter.com'});

You never send the request.

You need to use request.end()

NOTE: the request is not complete. This method only sends the header of the request. One needs to call request.end() to finalize the request and retrieve the response. (This sounds convoluted but it provides a chance for the user to stream a body to the server with request.write().)

Also the response event'S parameter is the response object NOT the body. You need to set the data event on the response object and then listen for the end event to make sure you got all the data.

request.on('response', function (response) {
  var body = '';
  response.on('data', function (chunk) {
    body += chunk;
  });
  response.on('end', function () {
    console.log('BODY: ' + body);
  });
});
request.end(); // start the request

See: http://nodejs.org/api/http.html#http_class_http_clientrequest

A few more tips

  1. You might want to use querystring.escape to urlencode your search parameter
  2. You also need to set the Host header, otherwise Twitter will return a 404

Fixed code:

var querystring = require('querystring');
...
var request = client.request("GET", "/search.json?q=" + querystring.escape(query), {'Host': 'search.twitter.com'});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文