节点的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();
- npm链接与git风格的子命令
- 如何使用TypeScript制作一个shell可执行节点文件
- 节点js从文件获取文件夹path
- 如何继续在repl / bash w / Node.js Commander中接收asynchronousconsole.log消息?
- npm install -g cordova。 这么多的错误。 请帮助
- 我的eslint不能在带有* .jspath参数的窗口中工作
- 节点bin文件在linux或osx上不被识别
- JavaScript – 使用JasminetestingNodeJS readline.createInterface的实现
- 同步subprocess调用Node.js 0.10.x中的shell脚本(包括CLI)