问题
I've been doing this to synchronously read the whole stdin data under Linux:
var buffer = fs.readFileSync('/dev/stdin');
This obviously won't work on Windows since there is no /dev/stdin file. What could I do to achieve the same?
回答1:
The module readline-sync do the job very well.
npm install readline-sync
and then:
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
回答2:
var size = fs.fstatSync(process.stdin.fd).size;
var buffer = size > 0 ? fs.readSync(process.stdin.fd, size)[0] : '';
来源:https://stackoverflow.com/questions/8452957/synchronously-reading-stdin-in-windows