javascript - How to signal EOF to node.js stdin from console? -


i have simple node.js app echo stdin. when run interactively on windows console, expected control-z recognised eof signal. isn't. how node app treat control-z eof?

// testecho.js  process.stdin.setencoding('utf-8'); console.log("input tty?:",process.stdin.istty);  process.stdin.on('readable',function() {     var vtext = process.stdin.read();     if (vtext != null)         console.log('echo: "%s"',vtext);     process.stdout.write('> '); // prompt next     });  process.stdin.on('end',function() { // works redirected input not triggered ^z on tty     console.log('end of input reached');     }); 

```

the problem you're using process.stdin.on instead of process.on()

see fix made here , should fine , dandy :) enjoy!

process.stdin.setencoding('utf-8'); console.log("input tty?:", process.stdin.istty);  process.stdin.on('readable',function() {     var vtext = process.stdin.read();     if (vtext != null)         console.log('echo: "%s"',vtext);     process.stdout.write('> '); // prompt next });  process.on('sigint', function () {   console.log('over , out!');   process.exit(0); }); 

also replaced 'end' 'sigint' that's signal caught ctrl+c

you can read signal events here: https://nodejs.org/api/process.html#process_signal_events


Comments