我想使用node.js发出HTTP请求,从Web服务器加载一些文本.由于响应可以包含很多文本(几兆字节),我想分别处理每个文本块.我可以使用以下代码实现此目的:
var req = http.request(reqOptions, function(res) {
...
res.setEncoding('utf8');
res.on('data', function(textChunk) {
// process utf8 text chunk
});
});
Run Code Online (Sandbox Code Playgroud)
这似乎没有问题.但是我想支持HTTP压缩,所以我使用zlib:
var zip = zlib.createUnzip();
// NO res.setEncoding('utf8') here since we need the raw bytes for zlib
res.on('data', function(chunk) {
// do something like checking the number of bytes downloaded
zip.write(chunk); // give the raw bytes to zlib, s.b.
});
zip.on('data', function(chunk) {
// convert chunk to utf8 text:
var textChunk = chunk.toString('utf8');
// process utf8 text chunk
});
Run Code Online (Sandbox Code Playgroud)
对于'\u00c4' …
node.js ×1