Unable to fire "end" event using CTRL D when reading from stdin

In the following code

process.stdin.resume(); process.stdin.setEncoding('utf8'); process.stdin.on('data', function(chunk) { process.stdout.write('data: ' + chunk); }); process.stdin.on('end', function() { process.stdout.write('end'); }); 

I cannot fire the "end" event using ctrl + D, and ctrl + C just exit without calling it.

 hello data: hello data data: data foo data: foo ^F data: β™  ^N data: β™« ^D data: ♦ ^D^D data: ♦♦ 
+6
source share
2 answers

I would change this:

 process.stdin.on('end', function() { process.stdout.write('end'); }); 

For this:

 process.on('SIGINT', function(){ process.stdout.write('\n end \n'); process.exit(); }); 

Additional Resources: Process Documents

+4
source

I also ran into this problem and found the answer here: Github Question

The readline interface provided by the windows themselves (for example, the one you are currently using) does not support ^ D. If you want more unix-y behavior, use the readline built-in module and set stdin to raw mode. This will make node interpret the original keystrokes, and ^ D will work. See http://nodejs.org/api/readline.html .

If you are on Windows, the readline interface does not support ^ D by default. You will need to change this in accordance with the related instructions.

+5
source

Source: https://habr.com/ru/post/944374/


All Articles