我不明白为什么我不能在下面的代码中调用end
事件。我已经阅读了太多有关如何执行此操作的内容,并且我了解我需要将流(process.stdin)切换为流动模式。但是,如果我的代码中有一个data
事件,那么它不是自动处于流动模式吗?我已经尝试过process.stdin.resume()
和.pipe()
。我正在Node.js上运行它。
非常感谢。
// begin snippet: js hide: false console: true babel: false
// language: lang-js
var i=0;
process.stdin.resume();
process.stdin.setEncoding("utf-8");
var stdin_input = "";
process.stdin.on("data", function (input) {
stdin_input += input;
i++
if(i>5){
//do something to call "end" event at the bottom
i=0
}
});
process.stdin.on("end", function () {
console.log(stdin_input);
});
// end snippet