0

I use process.stdin.setEncoding('utf8'); But when i listen a event 'data', the text isn't egal.

process.stdin.on('data', function (text) {
  if (text === 'q') console.log('ouiiiiiiiiii');
  else console.log(text);
});

I type 'q' but don't display "ouiiiiiiiii", and the text is q... but the text === 'q' is to false, why ? thanks

I think it's due to the encoding, but i don't know.

4

1 回答 1

0

首先,process.stdin.resume()在标准输入上设置回调之前调用。另外,请确保将您的text变量转换为字符串,因为我相信它是一个Buffer对象。

所以,这样的事情应该有效:

process.stdin.resume();
process.stdin.setEncoding('utf8');

process.stdin.on('data', function (text) {
 if (text.toString().trim() === 'q') {
   console.log('ouiiiiiiiiii');
 } else {
   console.log(text.toString());
 }
});
于 2012-07-27T16:10:18.617 回答