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:
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user