timezone update

This commit is contained in:
Edison
2025-11-03 11:31:34 +08:00
parent b1a8612571
commit 7231310f93
4 changed files with 163 additions and 106 deletions
+24 -17
View File
@@ -1,4 +1,7 @@
// server.js
import dotenv from 'dotenv'; //load .env first before anything else
dotenv.config({ path: path.join(path.dirname(fileURLToPath(import.meta.url)), '.env') });
import express from 'express';
import cors from 'cors';
import https from 'https';
@@ -6,10 +9,10 @@ import http from 'http';
import fs from 'fs';
import path from 'path';
import { fileURLToPath } from 'url';
import dotenv from 'dotenv';
import mysql from 'mysql2/promise';
import managerRoutes from './managerRoutes.js';
import workerRoutes from './workerRoutes.js';
import { APP_TIMEZONE } from './config/db.js';
async function startServer() {
dotenv.config({ path: path.join(path.dirname(fileURLToPath(import.meta.url)), '.env') });
@@ -17,15 +20,19 @@ async function startServer() {
const app = express();
const db = mysql.createPool({
host: process.env.DB_HOST,
user: process.env.DB_USER,
password: process.env.DB_PASSWORD,
database: process.env.DB_NAME,
port: process.env.DB_PORT,
waitForConnections: true,
connectionLimit: 10,
queueLimit: 0,
});
host: process.env.DB_HOST,
user: process.env.DB_USER,
password: process.env.DB_PASSWORD,
database: process.env.DB_NAME,
port: process.env.DB_PORT,
waitForConnections: true,
connectionLimit: 10,
queueLimit: 0,
// Return date/time columns as strings like "YYYY-MM-DD HH:mm:ss"
dateStrings: ['DATE', 'DATETIME', 'TIMESTAMP'],
});
try {
const connection = await db.getConnection();
@@ -54,24 +61,24 @@ async function startServer() {
credentials: true,
methods: ['GET', 'POST', 'PUT', 'DELETE', 'OPTIONS'],
allowedHeaders: ['Content-Type', 'Authorization', 'ngrok-skip-browser-warning'],
exposedHeaders: ['Content-Range', 'X-Content-Range'],
exposedHeaders: ['Content-Range', 'X-Content-Range', 'X-Export-TZ'],
};
app.use(cors(corsOptions));
app.use(express.json());
// --- Public server time endpoints (no auth, no cache) ---
const timeHandler = (req, res) => {
const now = new Date();
const ymdKL = new Intl.DateTimeFormat('en-CA', {
timeZone: 'Asia/Kuala_Lumpur',
year: 'numeric', month: '2-digit', day: '2-digit'
}).format(now); // "YYYY-MM-DD"
const now = new Date();
const ymdTZ = new Intl.DateTimeFormat('en-CA', {
timeZone: APP_TIMEZONE,
year: 'numeric', month: '2-digit', day: '2-digit'
}).format(now); // "YYYY-MM-DD"
res.set('Cache-Control', 'no-store, no-cache, must-revalidate, proxy-revalidate');
res.set('Pragma', 'no-cache');
res.set('Expires', '0');
res.json({ nowIso: now.toISOString(), tz: 'Asia/Kuala_Lumpur', ymdKL });
res.json({ nowIso: now.toISOString(), tz: APP_TIMEZONE, ymdTZ });
};
app.get('/time', timeHandler); // public path
app.get('/api/time', timeHandler); // also under /api