简体   繁体   中英

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?

PS 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)

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

eg

res.on('end', function () {
    // now I can process the data
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM