从标准input读取时,不能使用CTRL D触发'结束'事件

在下面的代码中

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触发'end'事件,而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(); }); 

更多资源: 处理文档

我也遇到了这个问题,并在这里find答案: Github的问题

由Windows本身提供的readline接口(例如,您正在使用的接口)不支持^ D。 如果您想要更多的unix-y行为,请使用readline内置模块并将stdin设置为原始模式。 这将使节点解释原始按键和^ D将工作。 请参阅http://nodejs.org/api/readline.html

如果您在Windows上,则readline界面默认不支持^ D。 您将需要根据链接说明更改。