节点的process.stdin可读streamlogging在可读事件侦听器callback中读取时为空

在不使用data事件的情况下,我希望此代码logging每个密钥的unicode引用,因为我将其按下。 我不明白为什么我每次都得到Null

每当我按下我的键盘上的一个键,我会触发一个可读的事件process.stdin运行一个callback,它允许我从这个可读的stream中读取数据。 那么为什么它没有从我的按键中保存任何数据呢?

 // nodo.js function nodo() { var stdin = process.stdin; var stdout = process.stdout; if (stdin.isTTY) { stdin.setRawMode(true); stdin.setEncoding('utf8'); stdin.resume(); stdout.write('\u000A>Bienvenido\u000A'); } else { process.exit(); } stdin.on('readable', function(){ var input = stdin.read(); console.log(input); }); } nodo(); 

运行代码

我感谢你的关注。

请阅读该文档 ,说明如何正确处理process.stdin 。 你的错误是使用stdin.resume启用进程stdinstream的“旧”兼容模式。

 // nodo.js function nodo() { var stdin = process.stdin; var stdout = process.stdout; if (stdin.isTTY) { stdin.setRawMode(true); stdin.setEncoding('utf8'); stdout.write('\u000A>Bienvenido\u000A'); process.stdin.setEncoding('utf8'); process.stdin.on('readable', function() { var chunk = process.stdin.read(); if (chunk !== null) { process.stdout.write('data: ' + chunk); } }); process.stdin.on('end', function() { process.stdout.write('end'); }); } else { process.exit(); } } nodo();