Попробуйте сделать свой планировщик вне маршрута, в вашем примере ваш планировщик будет запускаться каждый раз при попадании в маршрут, что не требуется,
Например,
_count = 0;
function scheduleDatabaseWrite() {
const { year, month, day, today } = GetCurrentDateMonthYear();
schedule.scheduleJob({ hour: 23, minute: 59, second: 1 }, () => {
connection.query(
`INSERT INTO visiter VALUES (${year}, ${month + 1},${day}, ${count});`
);
_count = 0; //And do this re-initialization only if the database write is success
});
}
function GetCurrentDateMonthYear() {
var date = new Date();
let year = date.getFullYear();
let month = date.getMonth(); // also you might need to check this logic as well, you want js month index or real world number, date.getMonth() - will return value starting from 0 and end's at 11, if you want 1 - 12 then add 1 to it, like date.getMonth()+1
let day = date.getDate();
var today = year + " " + month + " " + day;
return { year, month, day, today };
}
scheduleDatabaseWrite();
p.get('/', (req, res) => {
const { year, month, day, today } = GetCurrentDateMonthYear();
console.log(req.session.lastVisit);
if (req.session.lastVisit != today) {
req.session.lastVisit = today;
_count++;
}
fs.readFile(path.join(__dirname, 'html', 'index.html'), (err, data) => {
try {
res.writeHead(200, { 'Content-Type': 'text/html' });
console.log(count);
res.end(data);
} catch (err) {
res.writeHead(404, () => {
console.log("Page Not Found!")
});
}
})
});