Sunday, 12 March 2017

Why 'readable' event won´t fire for my ClientRequest?

I am testing a small chunk of code on NodeJS that is supposed to create a ClientRequest object and fire three event:

  • 'response' event
  • 'readable' event
  • 'end' event

Here is a chunk of code I am testing:

var http = require('http');
var server = http.createServer();
var link = "www.asos.com";

// now that server is running
server.listen(1337, '127.0.0.1', function(){
  tryMe();
});

 function tryMe(){
    var options = {
      hostname: link,
      port: 80,
      method: "GET"
    };
    var asosRequest = http.request(options);
    asosRequest.on("response", function(res){
      console.log("response");
    });
    asosRequest.on('readable', function() {
      console.log("readable");
    });
    asosRequest.on('end', function(){
      console.log("Bey!");
    });
    asosRequest.end();
}

I cannot figure out why the "readable" and "end" events won´t fire in my case. "response" event works fine and returns correct info and data buffers. Is there something I misunderstand about how data streams work in node?



via margarita

No comments:

Post a Comment