fix: serve static files in production

- Added static file serving in Express for production
- Added SPA fallback for client-side routing
- Created Dockerfile with NODE_ENV=production
- Frontend now properly served at dash.descomplicar.pt

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
2026-02-04 23:43:36 +00:00
parent 75f29ee6d5
commit 4af01c0f36
3 changed files with 52 additions and 0 deletions

View File

@@ -5,6 +5,8 @@
import 'dotenv/config'
import express from 'express'
import cors from 'cors'
import path from 'path'
import { fileURLToPath } from 'url'
import dashboardRouter from './routes/dashboard.js'
import monitorRouter from './routes/monitor.js'
import diagnosticRouter from './routes/diagnostic.js'
@@ -12,8 +14,12 @@ import hetznerRouter from './routes/hetzner.js'
import wpMonitorRouter from './routes/wp-monitor.js'
import serverMetricsRouter from './routes/server-metrics.js'
const __filename = fileURLToPath(import.meta.url)
const __dirname = path.dirname(__filename)
const app = express()
const PORT = process.env.API_PORT || 3001
const isProduction = process.env.NODE_ENV === 'production'
// Middleware
app.use(cors({
@@ -35,6 +41,20 @@ app.use('/api/hetzner', hetznerRouter)
app.use('/api/wp-monitor', wpMonitorRouter)
app.use('/api/server-metrics', serverMetricsRouter)
// Serve static files in production
if (isProduction) {
const distPath = path.join(__dirname, '..', 'dist')
app.use(express.static(distPath))
// SPA fallback - serve index.html for all non-API routes
app.get('*', (req, res, next) => {
if (req.path.startsWith('/api')) {
return next()
}
res.sendFile(path.join(distPath, 'index.html'))
})
}
// Error handling
app.use((err: any, _req: express.Request, res: express.Response, _next: express.NextFunction) => {
console.error('Server error:', err)