我目前正在学习 node.js 流,因此我想编写自己的自定义流模块。这是我目前所拥有的:
index.js:
fs = require("fs")
var module = require("./module")
var ws = fs.createWriteStream("out.txt")
fs.createReadStream("in.txt")
.pipe(module())
.pipe(ws)
模块.js:
var Stream = require('stream').Stream;
module.exports = function() {
var stream = new Stream();
this.writable = true;
this.readable = true;
stream.write = function(data) {
console.log("write")
stream.emit("data", data)
}
stream.end = function() {
console.log("end")
stream.emit("end")
}
stream.destroy = function() {
console.log("destroy")
stream.emit("close")
}
return stream;
}
在.txt:
text
text
text
text
text
text
text
text
text
text
问题是它out.txt
是空的并且该stream.write
函数永远不会被调用。为什么呢?如果我注释掉我的中间管道一切正常!