我正在node.js中构建一个应用程序,它会加载几页来分析内容.
因为node.js发送块我可以分析块.如果一个块包含例如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);
}); …Run Code Online (Sandbox Code Playgroud)