Node.js 事件流是否支持nodejs中的非utf-8编码?
我正在尝试按如下方式逐行上传和解析文件:Node.js 事件流是否支持nodejs中的非utf-8编码?,node.js,pipe,chunked-encoding,event-stream,Node.js,Pipe,Chunked Encoding,Event Stream,我正在尝试按如下方式逐行上传和解析文件: var fs = require('fs'), es = require('event-stream'), filePath = './file.txt'; fs.createReadStream(filePath) .pipe(new Iconv('cp866', 'windows-1251')) .pipe(es.split("\n")) .pipe(es.map(function (line, cb) { /
var fs = require('fs'),
es = require('event-stream'),
filePath = './file.txt';
fs.createReadStream(filePath)
.pipe(new Iconv('cp866', 'windows-1251'))
.pipe(es.split("\n"))
.pipe(es.map(function (line, cb) {
//do something with the line
cb(null, line)
}))
.pipe(res);
但不幸的是,我得到了utf-8编码的“行”字符串。是否可以防止事件流更改编码?事件流基于较旧版本的流。在国际海事组织,它试图做的太多了 我会使用
到2
。但这需要一些额外的工作
var fs = require('fs'),
thr = require('through2'),
filePath = './file.txt';
fs.createReadStream(filePath)
.pipe(new Iconv('cp866', 'windows-1251'))
.pipe(thr(function(chunk, enc, next){
var push = this.push
chunk.toString().split('\n').map(function(line){
// do something with line
push(line)
})
next()
}))
.pipe(res);
显然,您可以避免调用toString()
,自己操作缓冲区
through2
是一个围绕流的薄包装。Transform
提供了更多的控制