admin管理员组文章数量:1317909
I'm reading this article about gathering request data and it gives the following example:
var body = [];
request.on('data', function(chunk) {
body.push(chunk);
}).on('end', function() {
body = Buffer.concat(body).toString();
// at this point, `body` has the entire request body stored in it as a string
});
Other tutorials suggest this way:
var total = [];
request.on('data', function(chunk) {
total += chunk;
}).on('end', function() {
body = total.toString();
// at this point, `body` has the entire request body stored in it as a string
});
They seem to be equivalent. Why use more elaborate Buffer.concat(body).toString();
then?
I'm reading this article about gathering request data and it gives the following example:
var body = [];
request.on('data', function(chunk) {
body.push(chunk);
}).on('end', function() {
body = Buffer.concat(body).toString();
// at this point, `body` has the entire request body stored in it as a string
});
Other tutorials suggest this way:
var total = [];
request.on('data', function(chunk) {
total += chunk;
}).on('end', function() {
body = total.toString();
// at this point, `body` has the entire request body stored in it as a string
});
They seem to be equivalent. Why use more elaborate Buffer.concat(body).toString();
then?
1 Answer
Reset to default 9Why use
Buffer.concat(body).toString();
instead ofUintArray8.toString()
?
Because they're doing totally different things. But that's not your real question, chunk
is a Buffer
as well not an Uint8Array
.
The two ways of gathering request data seem to be equivalent. What's the difference?
The second snippet is absolutely horrible code. Don't use it. First of all, it should have been written like this:
var total = "";
request.on('data', function(chunk) {
total += chunk.toString();
}).on('end', function() {
// at this point, `total` has the entire request body stored in it as a string
});
Starting with an array is absolute nonsense if you're doing string concatenation on it, and total.toString()
was only necessary for the case that there were no data
events. total
would better be a string right from the beginning. In chunk.toString()
, the explicit method call is unnecessary (omitting it would have led to it being called implicitly), but I wanted to show what happens here.
Now, how is converting the chunk
buffers to strings and concatenating them different from collecting the buffers in an array, concatenating them to a big buffer and converting that to a string?
The answer is multiple-byte characters. Depending on the encoding and body text, there might be characters that are represented by multiple bytes. It can happen that those bytes e to lie across the border of two chunks (in subsequent data
events). With the code that decodes each chunk separately, you'll get an invalid result in those cases.
本文标签: javascriptWhy use Bufferconcat(body)toString() instead of Uint8ArrayBuffertoString()Stack Overflow
版权声明:本文标题:javascript - Why use `Buffer.concat(body).toString();` instead of `Uint8ArrayBuffer.toString()` - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742027580a2415836.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论