javascript - Node.js HTTP request returns 2 chunks (data bodies) - Stack Overflow

I'm trying to get the source of an HTML file with an HTTP request in node.js - my problem is that

I'm trying to get the source of an HTML file with an HTTP request in node.js - my problem is that it returns data twice. Here is my code:

var req = http.request(options, function(res) {
    res.setEncoding('utf8');
    res.on('data', function (chunk) {
        if(chunk.length > 1000) {
            console.log(chunk.length);
        }
    });
    req.on('error', function(e) {
        console.log("error" + e.message);
    });
});

req.end();

This then returns:

5637
3703

The hell! When I just console.log(chunk), it returns all the data as if it were one large string, and when I add a something like console.log("data starts here") in the res.on('data', it returns the whole string with the "data starts here" somewhere in the middle, implying it's just being split.

Every test I do returns 2 values and it's really annoying. I can just do "if(chunk.length > 4000)" but given the nature of the page I'm getting, this could change. How can I make it so that all the data returns in one large chunk?

I'm trying to get the source of an HTML file with an HTTP request in node.js - my problem is that it returns data twice. Here is my code:

var req = http.request(options, function(res) {
    res.setEncoding('utf8');
    res.on('data', function (chunk) {
        if(chunk.length > 1000) {
            console.log(chunk.length);
        }
    });
    req.on('error', function(e) {
        console.log("error" + e.message);
    });
});

req.end();

This then returns:

5637
3703

The hell! When I just console.log(chunk), it returns all the data as if it were one large string, and when I add a something like console.log("data starts here") in the res.on('data', it returns the whole string with the "data starts here" somewhere in the middle, implying it's just being split.

Every test I do returns 2 values and it's really annoying. I can just do "if(chunk.length > 4000)" but given the nature of the page I'm getting, this could change. How can I make it so that all the data returns in one large chunk?

Share Improve this question edited Nov 16, 2015 at 23:17 Brian Tompsett - 汤莱恩 5,89372 gold badges61 silver badges133 bronze badges asked Feb 25, 2014 at 13:29 Alex BassAlex Bass 2,4621 gold badge15 silver badges16 bronze badges 2
  • The last one might be a request for favicon.ico – pstenstrm Commented Feb 25, 2014 at 13:32
  • This is perfectly normal behavior... console.log(chunk)'ll give you the request results as they arrive. – soyuka Commented Feb 25, 2014 at 13:34
Add a ment  | 

1 Answer 1

Reset to default 4

These are not "2 data bodies", these are 2 chunks(pieces) of the same body, you have to concatenate them.

var req = http.request(options, function(res) {

    var body = '';

    res.setEncoding('utf8');

    // Streams2 API
    res.on('readable', function () {
        var chunk = this.read() || '';

        body += chunk;
        console.log('chunk: ' + Buffer.byteLength(chunk) + ' bytes');
    });

    res.on('end', function () {
        console.log('body: ' + Buffer.byteLength(body) + ' bytes');
    });

    req.on('error', function(e) {
        console.log("error" + e.message);
    });
});

req.end();

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745357480a4624206.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信