在Windows中同步读取stdin

我一直在这样做,在Linux下同步读取整个stdin数据:

var buffer = fs.readFileSync('/dev/stdin'); 

这显然不能在Windows上运行,因为没有/ dev / stdin文件。 我能做些什么来达到同样的效果?

Solutions Collecting From Web of "在Windows中同步读取stdin"

模块readline-sync很好地完成了这项工作。

 npm install readline-sync 

接着:

 var readlineSync = require('readline-sync'); var answer = readlineSync.question('What is your favorite food? :'); console.log('Oh, so your favorite food is ' + answer); 

https://www.npmjs.com/package/readline-sync

 var size = fs.fstatSync(process.stdin.fd).size; var buffer = size > 0 ? fs.readSync(process.stdin.fd, size)[0] : '';