Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

request.on("response",[…]) never gets fired

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.

like image 323
nocksock Avatar asked Dec 09 '10 13:12

nocksock


People also ask

What is HTTP createServer?

The http. createServer() method turns your computer into an HTTP server. The http. createServer() method creates an HTTP Server object. The HTTP Server object can listen to ports on your computer and execute a function, a requestListener, each time a request is made.

What is RES on?

res. on('data', ...) is how you register a listener for the data event and the data event is the primary way that you receive data from the incoming stream. This listener will be called one or more times with chunks of arriving data.

What is setHeader in node js?

setHeader(name, value) (Added in v0. 4.0) method is an inbuilt application programming interface of the 'http' module which sets a single header value for implicit headers. If this header already exists in the to-be-sent headers, its value will be replaced.


1 Answers

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'});
like image 129
Ivo Wetzel Avatar answered Nov 13 '22 18:11

Ivo Wetzel