0

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

EEAH
  • 715
  • 4
  • 17

2 Answers2

1

Why "Handler started" and "Process terminated" are not printed to the console ?

When I am running the below script, I am getting things printed on console. I think you should run the below script and test it:

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', () => {
  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)
  }
}

Results after running the above script:

AV:newTest11 apoorvachikara$ node test.js 
0
1
2
3
4
6
7
8
9
Server ready
Process terminated
AV:newTest11 apoorvachikara$ 

It seems there are some issues with the script you are running.

Apoorva Chikara
  • 8,277
  • 3
  • 20
  • 35
  • Yes but why listening on a port is required for this to work ? It should enter the handler in the question code – EEAH Dec 07 '21 at 15:08
  • Process will only be available when you start the server. Once you get the process, you can send different signals. – Apoorva Chikara Dec 07 '21 at 15:35
  • Turned out the error is in the process.kill method. Updated the question. Even when listening to a server the error is still hapenning – EEAH Dec 07 '21 at 15:35
  • What node version are you using? Because running the same code on my machine, produces the same o/p as mentioned in the answer. – Apoorva Chikara Dec 07 '21 at 15:38
  • version 16.13.0. (running on windows 10) – EEAH Dec 07 '21 at 15:40
  • When you run the above script, what is the o/p you are getting on your machine? – Apoorva Chikara Dec 07 '21 at 15:53
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/239917/discussion-between-eeah-and-apoorva-chikara). – EEAH Dec 07 '21 at 15:55
0

Turned out that the windows os is not POSIX so it doesn't use unix signals.

The best thing to do is to either use the windows subsystem for linux (WSL2) to develop on a unix environment using windows or develop node js application on unix os

EEAH
  • 715
  • 4
  • 17