I am new to node.js and following the node.js documentation about process signals i've written the below code
const express = require("express");
const app = express();
app.get("/", (req, res) => {
res.send("Hi!");
});
const server = app.listen(3000, () => console.log("Server ready"));
process.on("SIGTERM", () => {
console.log("Handler started");
server.close(() => {
console.log("Process terminated");
});
});
for (let i = 0; i < 10; i++) {
if (i === 5) {
process.kill(process.pid, "SIGTERM");
} else {
console.log(i);
}
}
but after running this app in the terminal the output (node app.js
) is the following:
0
1
2
3
4
The handler didn't run
After debugging the process is exiting with code = 1 when running process.kill
.
How to debug this to know the exact cause ? I couldn't print the error with a try catch block