admin管理员组文章数量:1422048
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
1 Answer
Reset to default 4These 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();
本文标签: javascriptNodejs HTTP request returns 2 chunks (data bodies)Stack Overflow
版权声明:本文标题:javascript - Node.js HTTP request returns 2 chunks (data bodies) - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745357480a2655141.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论