Tuesday, 2 October 2018

node.js: readline: SIGINT event

The 'SIGINT' event is emitted whenever the input stream receives a <ctrl>-C input.

HelloWorld.js
const readline = require('readline');

const rl = readline.createInterface({
    input: process.stdin,
    output: process.stdout,
    prompt: ">"
});

rl.prompt();

rl.on('line', (line) => {
    console.log(`${line}`);
    rl.prompt();
});

rl.on('SIGINT', () => {
    rl.question('Do you want to exit? ', (answer) => {
        if (answer.match(/^y(es)?$/i)) rl.close();
    });
});

rl.on('close', () => {
    console.log("Closing the console");
});

Type CTRL + c to receive SIGINT event.



Previous                                                 Next                                                 Home

No comments:

Post a Comment