小编Fer*_*bus的帖子

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

我正在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)

javascript node.js

5
推荐指数
1
解决办法
8572
查看次数

标签 统计

javascript ×1

node.js ×1