admin管理员组

文章数量:1406937

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',
    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?

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.',
    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?

Share Improve this question edited Jul 17, 2015 at 0:54 Ben 57.4k50 gold badges184 silver badges229 bronze badges asked Jan 22, 2013 at 13:25 Ferry KobusFerry Kobus 2,0392 gold badges14 silver badges18 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 2

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.

本文标签: javascriptNodejs Howto close responserequest while retrieving data (chuncks)Stack Overflow