proper way of handling fatal errors in Node.js

SIGTERM is the signal that tells a process to gracefully terminate. It is the signal that's sent from process managers like upstart or supervisord and many others.

You can send this signal from inside the program, in another function: read more

process.kill(process.pid, 'SIGTERM')

You can also setup your own listeners for this event, and perform other tasks.

process.on('SIGTERM', () => {
  server.close(() => {
    console.log('Shutting down the application ...')
  })
})

But SIGTERM is not supported on windows, so you can use SIGINT instead, here is a useful info about SIGINT

After the SIGINT signal is received, the Node.js server initiates a shutdown sequence and enters shutdown mode. All of the existing requests are completed and no new requests are entertained. When the last request completes, the server closes all of the connections and shuts down. read more

Mor about processes here