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.
First, call process.stdin.resume()
before you setup your callback on stdin. Also, make sure to convert your text
variable to a string, as I believe it's a Buffer
object.
So, something like this should work:
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());
}
});