简体   繁体   English

Node.js:如何在检索数据时关闭响应/请求(chuncks)

[英]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. 我正在node.js中构建一个应用程序,它会加载几页来分析内容。

Because node.js sends chunks I can analyse the chunks. 因为node.js发送块我可以分析块。 If a chunk contains for example index,nofollow I want to close that connection and go on with the rest. 如果一个块包含例如index,nofollow我想关闭该连接并继续其余部分。

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(); 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). 不起作用..它继续检索数据/块...(在我的测试中我得到14个块,但在第一个块我已经知道我不需要其他块,所以我想要退出请求/响应)。

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 haven't tested it yet, putting this in your else block should work: res.removeAllListeners('data'); 我还没有测试过,把它放在你的else块中应该可以工作: res.removeAllListeners('data');

Basically your res is a child of EventEmitter Object. 基本上你的resEventEmitter Object的EventEmitter 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. 通过调用removeAllListeners('data') ,将删除绑定到data事件的所有处理程序,并且不再执行回调。 But you will still have to wait for all the data events to pass before end or error events are emitted on the request. 但是,在end或在请求上发出error事件之前,您仍然必须等待所有数据事件通过。

Also read nodejs EventEmitter documentation for more info. 另请阅读nodejs EventEmitter文档以获取更多信息。

Update: 更新:

You might as well try and emit end or close event on res object in your else block, like this: res.emit('end'); 您也可以尝试在else块中的res对象上发出endclose事件,如下所示: res.emit('end'); or res.emit('close'); res.emit('close'); . The documentation on clientRespone object for end event says that it is 有关end event的clientRespone对象的文档说明了它

Emitted exactly once for each response. 每个响应只发出一次。 After that, no more 'data' events will be emitted on the response. 之后,不会再在响应上发出“数据”事件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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