在下面的代码
process.stdin.resume();
process.stdin.setEncoding('utf8');
process.stdin.on('data', function(chunk) {
process.stdout.write('data: ' + chunk);
});
process.stdin.on('end', function() {
process.stdout.write('end');
});
使用CTRL + d,并且CTRL + C只是退出,而不触发它,我不能触发“结束”事件。
hello
data: hello
data
data: data
foo
data: foo
^F
data: ♠
^N
data: ♫
^D
data: ♦
^D^D
data: ♦♦
我会改变这样的:
process.stdin.on('end', function() {
process.stdout.write('end');
});
为此:
process.on('SIGINT', function(){
process.stdout.write('\n end \n');
process.exit();
});
更多资源: 处理文档
我也突然产生这个问题,在这里找到了答案: Github的问题
由Windows本身提供的readline的接口(例如,你现在正在使用的)不支持^ d。 如果你想要更多的UNIX-Y的行为,使用readline的内置模块和标准输入设为原始模式。 这将使节点解释原始按键和^ d会工作。 见http://nodejs.org/api/readline.html 。
如果您使用的是Windows,readline的接口不支持^ d默认。 您将需要更改每个链接的指令。
如果您在上下文Hackerrank codepair工具做那么这是给你的。
工具的工作方式是,你必须在标准输入部分输入一些输入,然后点击运行,将带你到标准输出。
所有在标准输入输入的输入的线路将由process.stdin.on(“数据”,函数(){})的码的一部分,并且被处理为一旦输入“端部”,将直接进入到处理.stdin.on(“结束”,函数(){})的一部分,其中我们可以做的处理,并使用process.stdout.write(“”),以输出该标准输出的结果。
process.stdin.resume();
process.stdin.setEncoding("ascii");
var input = "";
process.stdin.on("data", function (chunk) {
// This is where we should take the inputs and make them ready.
input += (chunk+"\n");
// This function will stop running as soon as we are done with the input in the Stdin
});
process.stdin.on("end", function () {
// When we reach here, we are done with inputting things according to our wish.
// Now, we can do the processing on the input and create a result.
process.stdout.write(input);
});
您可以通过在代码窗口中粘贴上他上面的代码检查流量。