To detect CTRL+C in Node.js, we can listen for the SIGINT event.
For instance, we write
process.on("SIGINT", () => {
console.log("Caught interrupt signal");
if (shouldExit) {
process.exit();
}
});
to listen for the SIGIN event with process.on
.
We call it with a callback that’s called when ctrl+c is pressed.
In it, we call process.exit
to exit the program if shouldExit
is true
.