Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Node.js http response end event?

I was using this piece of code to send some http requests and get data, and it was working fine. I recently updated apache and php to latest versions, as well as node.

And 'close' event stopped firing. I also tried 'end' and 'finish' none of this seems to be working.

I need to know when response is ended so I can start processing data, usually it comes in several chunks. Can you guys help?

var req = http.request(options, function(res) {
      res.on('data', function (chunk) {
        if(chunk != null && chunk != "") {
            dataString += chunk; c
        } 
      });
    });

    req.on('close', function () {
        //tadaa it is finished, so we can process dataString
    });

    req.write(post_data);
    req.end();

Current versions: Apache 2.4, PHP 5.4 node 0.10.9

Maybe there is some particular config settings of Apache that prevents it from closing connection?

P.S. I do not think it is Apache though.. I tried google.com with same result.. pretty strange... Anyone have a working code example? (load big data, and know when it ended)

like image 878
Avetis Zakharyan Avatar asked Jun 03 '13 13:06

Avetis Zakharyan


1 Answers

You should be waiting for the end event of the response, not the request.

e.g.

res.on('end', function () {
    // now I can process the data
});
like image 107
Joe Avatar answered Oct 12 '22 20:10

Joe