我正在用 node.js 构建一个应用程序,它加载多个页面并分析内容。
因为 node.js 发送块,所以我可以分析这些块。如果一个块包含例如索引,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);
});
我唯一无法弄清楚的是退出该连接。或者停止检索数据,因为我不需要它..
请求结束();不起作用..它继续继续检索数据/块..
(在我的测试中,我得到了 14 个块,但在第一个块中我已经知道我不需要其他块,所以我想退出请求/响应)。
我现在有一个布尔值,可以跳过分析其他块,但在我看来,我最好跳过检索数据?
调用什么函数?或者它是不可能的,因为它需要检索所有内容?