Node.js HTTP request returns 2 chunks (data bodies

2019-09-02 03:14发布

问题:

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?

回答1:

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();