nodejs stream 创建读写流
Nodejs 创建 读写 stream
2023-09-14 08:58:44 时间
const fs = require("fs");
const { Writable, Readable, Duplex, Transform } = require("stream");
// 双向流
const inoutStream = new Duplex({
// 获取写入的数据
write(chunk, encoding, callback) {
console.log(chunk.toString());
callback();
},
// 一直读直到null为止
read(size) {
this.push(String.fromCharCode(this.currentCharCode++));
if (this.currentCharCode > 90) this.push(null);
},
});
inoutStream.currentCharCode = 65;
process.stdin.pipe(inoutStream).pipe(inoutStream);
const sizeStream = new Transform({
readableObjectMode: true,
writableObjectMode: true,
// 转换流,可以改变数据
transform(chunk, encoding, callback) {
this.push(chunk.toString() + `\r\n // size(${chunk.length})`);
callback();
},
});
const outStream = new Writable({
write(chunk, encoding, callback) {
console.log(chunk.toString("utf-8"));
callback();
},
});
fs.createReadStream("./x.txt").pipe(sizeStream).pipe(outStream);
See also:
相关文章
- Nodejs+Extjs+Mongodb开发第一天 Nodejs环境搭建
- NodeJs的Event Loop
- nodeJs多进程Cluster
- NodeJs的Event Loop
- Pycharm调用NodeJS运行js代码 的 环境配置
- 使用nodejs代码在SAP C4C里创建Individual customer
- 使用nodejs创建Marketing Cloud的contact数据
- NodeJs——(16)用Nodejs 4.X版本,制作一个微博网站(多图,详细步骤)
- Linux(1)——在Linux下安装Nodejs(详细教程,包会),并成功创建一个简单的服务器
- NodeJs——(8)http.ServerRequest的过程
- nodejs之url模块