我正在阅读这篇关于收集请求数据的文章,它给出了以下示例:
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
});
其他教程建议这样做:
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
});
它们似乎是相等的。那么为什么要使用更精细的Buffer.concat(body).toString();
呢?
为什么用
Buffer.concat(body).toString();
代替UintArray8.toString()
?
因为他们在做完全不同的事情。但这不是你真正的问题,chunk
是Buffer
,也不是Uint8Array
。
收集请求数据的两种方法似乎是相同的。有什么区别?
第二个代码片段绝对是可怕的代码。不要用它。首先,它应该这样写:
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
});
如果你在数组上做字符串连接,从数组开始是绝对没有意义的,total.toString()
只有在没有data
事件的情况下才需要。total
最好从一开始就是字符串。在chunk.toString()
中,显式方法调用是不必要的(省略它会导致它被隐式调用),但是我想展示这里发生了什么。
现在,将chunk
缓冲区转换为字符串并连接它们与在数组中收集缓冲区,将它们连接到一个大缓冲区并将其转换为字符串有什么不同?
答案是多字节字符。根据编码和正文文本的不同,可能存在由多个字节表示的字符。这些字节可能会跨越两个块的边界(在随后的data
事件中)。使用单独解码每个块的代码,在这些情况下您将得到无效的结果。