如何使end事件在node.js中起作用

时间:2019-02-05 17:39:29

标签: javascript node.js node.js-stream

我不明白为什么我不能在下面的代码中调用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 

0 个答案:

没有答案