Я пытаюсь сохранить содержимое в файл, когда мой узел js (экспресс) сервер завершен, каким-то образом файл пуст.
function exitHandler() {
//.......
fs.writeFile('graph.txt', Graph, 'utf8', (err) => {
if (err != null) {
console.error(err);
}
});
}
// Bring in our dependencies
const express = require('express');
global.fs = require('fs');
// Connect all our routes to our application.
app = express();
// Turn on the server!
app.listen(3000, () => {
console.log('App listening on port 3000');.
});
// Catches exit event
process.on('exit', exitHandler.bind(null));
// Catches ctrl+c event
process.on('SIGINT', () => {
exitHandler();
process.exit(-1);
});
// Catches "kill pid" (for example: nodemon restart)
process.on('SIGUSR1', exitHandler.bind(null));
process.on('SIGUSR2', exitHandler.bind(null));
// Catches uncaught exceptions
process.on('uncaughtException', exitHandler.bind(null)
);
Если я удаляю process.exit(-1)
, он сохраняет содержимое в файл, но никогда не выходит из программы и вызывает событие 'exit'
.
спасибо