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 badges1 Answer
Reset to default 2I 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
版权声明:本文标题:javascript - Node.js: Howto close responserequest while retrieving data (chuncks) - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744972829a2635336.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论