From eb971b45081706fc0e619317c66f6efec11f220c Mon Sep 17 00:00:00 2001 From: chris Date: Wed, 30 Jul 2025 08:19:30 -0400 Subject: [PATCH] move index to public folder --- {images => public/images}/icon-512.png | Bin index.html => public/index.html | 0 server.js | 22 ++++++++++++++++------ 3 files changed, 16 insertions(+), 6 deletions(-) rename {images => public/images}/icon-512.png (100%) rename index.html => public/index.html (100%) diff --git a/images/icon-512.png b/public/images/icon-512.png similarity index 100% rename from images/icon-512.png rename to public/images/icon-512.png diff --git a/index.html b/public/index.html similarity index 100% rename from index.html rename to public/index.html diff --git a/server.js b/server.js index 4fc9629..d49ac37 100644 --- a/server.js +++ b/server.js @@ -3,7 +3,7 @@ require('dotenv').config(); const express = require('express'); const sqlite3 = require('sqlite3').verbose(); -const bcrypt = require('bcryptjs'); +const bcrypt =require('bcryptjs'); const jwt = require('jsonwebtoken'); const cors = require('cors'); const path = require('path'); @@ -18,6 +18,10 @@ const app = express(); app.use(cors()); app.use(express.json()); +// --- NEW: Serve static files from the 'public' directory --- +app.use(express.static(path.join(__dirname, 'public'))); + + const dbPath = path.resolve(__dirname, 'data', 'timetracker.db'); const db = new sqlite3.Database(dbPath, (err) => { if (err) console.error("Error opening database", err.message); @@ -42,7 +46,6 @@ function initializeDatabase() { } }); - // --- NEW: Clean up past time-off requests on server start --- const today = new Date().toISOString().split('T')[0]; db.run(`DELETE FROM time_off_requests WHERE end_date < ?`, [today], function(err) { if (err) { @@ -70,7 +73,9 @@ function authenticateToken(req, res, next) { }); } -// --- API Routes --- +// --- ALL /api/... ROUTES GO HERE (Code unchanged) --- +// (Your existing API routes for login, punch, admin, etc.) + app.post('/api/login', (req, res) => { const { username, password } = req.body; db.get('SELECT * FROM users WHERE username = ?', [username], (err, user) => { @@ -139,8 +144,6 @@ app.get('/api/user/time-off-requests', authenticateToken, (req, res) => { }); }); - -// --- Admin Routes --- app.post('/api/admin/create-user', authenticateToken, requireRole('admin'), (req, res) => { const { username, password, role } = req.body; const userRole = (role === 'admin' || role === 'employee') ? role : 'employee'; @@ -280,7 +283,6 @@ app.post('/api/admin/update-time-off-status', authenticateToken, requireRole('ad }); }); -// --- NEW: Route to delete a time-off request --- app.delete('/api/admin/time-off-requests/:id', authenticateToken, requireRole('admin'), (req, res) => { db.run('DELETE FROM time_off_requests WHERE id = ?', [req.params.id], function(err) { if (err) return res.status(500).json({ message: "Failed to delete request." }); @@ -289,4 +291,12 @@ app.delete('/api/admin/time-off-requests/:id', authenticateToken, requireRole('a }); }); + +// --- NEW: Add a catch-all route to serve the SPA --- +// This should come after all API routes +app.get('*', (req, res) => { + res.sendFile(path.join(__dirname, 'public', 'index.html')); +}); + + app.listen(PORT, () => console.log(`Server running on http://localhost:${PORT}`)); \ No newline at end of file