I'm trying to write code that will return whether the server is already running or not. However, my tests are not working.
I want on process.exit
to write a file that says the server is no longer running, and for when the server starts up, it to write that the server is running:
const { readdir, writeFile, writeFileSync, readFileSync } = require('fs')
const IS_RUNNING_LOCATION = `${__dirname}/running.txt`
if (readFileSync(IS_RUNNING_LOCATION).toString() === 'true')
return
process.on('exit', () => {
writeFileSync(IS_RUNNING_LOCATION, 'false', {}, (err) => {
if (!err) return console.log('written')
console.log(err)
})
})
writeFileSync(IS_RUNNING_LOCATION, 'true')
setInterval(()=> console.log('running'), 1000)
However, with this code, when I exit with ^C, the on exit code is not run.
Can anyone tell me why this is the case or how I can fix it?