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 can't trigger the 'end' event using ctrl+D, and ctrl+C just exit without triggering it.
hello data: hello data data: data foo data: foo ^F data: ♠ ^N data: ♫ ^D data: ♦ ^D^D data: ♦♦
Alternatively, after typing the input into shell, you can press Ctrl + D to send EOF (end-of-file) to trigger the event handler in process. stdin. on("end", ...)
process. stdin. resume() A Readable Stream that points to a standard input stream (stdin). Standard input streams are paused by default, so you must call process.
I'd change this (key combo Ctrl+D
):
process.stdin.on('end', function() { process.stdout.write('end'); });
To this (key combo Ctrl+C
):
process.on('SIGINT', function(){ process.stdout.write('\n end \n'); process.exit(); });
Further resources: process docs
I too came upon this problem and found the answer here: Github issue
The readline interface that is provided by windows itself (e.g. the one that you are using now) does not support ^D. If you want more unix-y behaviour, use the readline built-in module and set stdin to raw mode. This will make node interpret raw keypresses 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 that per the linked instructions.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With