Why SIGTERM event handler wasn't called in my code example?

Viewed 466

Windows 10 x64
Node v12.19.0

Why wasn't called my handler of SIGTERM event after first opening http://localhost:3000/ in a browser? Application is terminated without executiong my handler (I don't see its console output).

// node v12.19.0
const http = require("http");
const server = http.createServer((req,res)=> {
    res.statusCode = 200;
    res.end("Hello, World!");
    setTimeout(() => { process.kill(process.pid, "SIGTERM"); } , 2000);
});

process.on("SIGTERM", () => { // Why it will not be executed?
    process.statusCode = 1;
    console.log("SIGTERM");
    server.close(() => {
        console.log("Process terminated.");
    });
});

server.listen(3000,()=>{
    console.log("Server works on http://localhost:3000/");  
});

My console output (PowerShell):

PS C:\tmp_src> node index.js
Server works on http://localhost:3000/
PS C:\tmp_src>
1 Answers

On Windows 10, process.on('SIGINT',cb) works for me, to catch interrupts like CTRL+C in a shell, or stop button commands in an IDE.

It also works well in production on Linux, particularly for pm2 restart and similar commands.

Related