Skip to content Skip to sidebar Skip to footer

Nodejs : Http Response Fired Twice

I'm beginning with Node JS, and currently doing Learnyounode tutorial. I'm into the HTTP moudle first step : getting content through http.get() request. So I made a very basic thin

Solution 1:

The "data" event does not guarantee that you will receive the entire message from the server in a single chunk. As pieces of the stream of data coming to your client are received, only those immediately available are handled by your function, in sequence. It may happen that the entire message arrives all at once, but the behavior isn't at all deterministic.

You can just merge the entire data into a single string, and then write it to the console.

var http = require('http')

http.get('http://www.vinylzilla.com/search/autocomplete?q=kiss', function(response){
    var content = '';
    response.on('data',function(data){
        content += String(data);
    });
    response.on('end', function() {
        console.log('Result: ", content);
    });
})

Post a Comment for "Nodejs : Http Response Fired Twice"