Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Node.js: Howto close response/request while retrieving data (chuncks)

I'm building an application in node.js wich loads several pages an analyses the contents.

Because node.js sends chunks I can analyse the chunks. If a chunk contains for example index,nofollow I want to close that connection and go on 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't figure out is to quit that connection. Or stop retrieving data because i don't need it..

req.end(); doesn't work.. it keeps continuing with retrieving data/chunks.. (in my test I get 14 chunks, but at the first chunk I already know i don't need the other chunks, so I want to quit the request/response).

I now have a boolean that skips analyzing the others chunks, but in my opinion I'm better of skipping retrieving the data?

What function to call? Or is it impossible because it needs to retrieve everything?

like image 311
Ferry Kobus Avatar asked Nov 03 '22 06:11

Ferry Kobus


1 Answers

I haven't tested it yet, putting this in your else block should work: res.removeAllListeners('data');

Basically your res is a child of EventEmitter Object. By calling removeAllListeners('data') on it, all the handlers which are bound to data event will be removed and the callback won't execute anymore. But you will still have to wait for all the data events to pass before end or error events are emitted on the request.

Also read nodejs EventEmitter documentation for more info.

Update:

You might as well try and emit end or close event on res object in your else block, like this: res.emit('end'); or res.emit('close');. The documentation on clientRespone object for end event says that it is

Emitted exactly once for each response. After that, no more 'data' events will be emitted on the response.

like image 137
Juzer Ali Avatar answered Nov 11 '22 06:11

Juzer Ali