Node.js: How to close a response / request when receiving data (chuncks)

I am creating an application in node.js that loads multiple pages, parses the contents.

Since node.js sends chunks, I can parse chunks. If the piece contains, for example, an index, nofollow, I want to close this connection and continue with the rest.

var host  = 'example.com',
    total = '',
    http  = require('http');

var req = http.request({hostname: host, port: 80, path: '/'}, function(res) {
    res.on('data', function(chunk) {
        total += chunk;
        if(chunk.toString().indexOf('index,nofollow') == -1) {
            // do stuff
        } else {
            /*
             * WHAT TO DO HERE???
             * how to close this res/req?
             * close this request (goto res.end or req.end????
             */
        }
    }).on('end', function() {
        // do stuff
    });
}).on('error', function(e) {
    // do stuff
    console.log("Got error: " + e.message);
});

The only thing I can not understand is to get out of this connection. Or stop receiving data because I do not need it.

req.end (); doesn't work .. it keeps on getting data / chunks .. (in my test I get 14 chunks, but in the first fragment I already know that I don’t need other chunks, so I want to exit the request / response).

Now I have a boolean that skips the analysis of the remaining blocks, but, in my opinion, should I skip data extraction?

? , ?

+5
1

, else, : res.removeAllListeners('data');

res EventEmitter Object. removeAllListeners('data') , , data, . , , end error.

nodejs EventEmitter .

:

end close res else, : res.emit('end'); res.emit('close');. clientRespone end ,

. "".

+2

All Articles