fix try
This commit is contained in:
@@ -127,9 +127,17 @@ app.use(express.urlencoded({ extended: true }));
|
||||
|
||||
// Serve frontend build files (if exists, for production)
|
||||
const path = require('path');
|
||||
const frontendDistPath = path.join(__dirname, '../../frontend/dist');
|
||||
const fs = require('fs');
|
||||
|
||||
// Determine frontend dist path
|
||||
// In single container: /app/backend/src -> /app/frontend/dist
|
||||
// In separate containers: backend/src -> ../../frontend/dist (won't exist, that's OK)
|
||||
const frontendDistPath = path.join(__dirname, '../../frontend/dist');
|
||||
|
||||
// Log the resolved path for debugging
|
||||
logger.info(`Frontend dist path resolved: ${frontendDistPath}`);
|
||||
logger.info(`__dirname: ${__dirname}`);
|
||||
|
||||
// Serve static files (landing page) - but only for specific paths
|
||||
// Don't serve from public if frontend dist exists (to avoid conflicts)
|
||||
if (!fs.existsSync(frontendDistPath)) {
|
||||
@@ -137,20 +145,33 @@ if (!fs.existsSync(frontendDistPath)) {
|
||||
}
|
||||
|
||||
if (fs.existsSync(frontendDistPath)) {
|
||||
logger.info(`Frontend dist found at: ${frontendDistPath}`);
|
||||
logger.info(`✅ Frontend dist found at: ${frontendDistPath}`);
|
||||
|
||||
// List files for debugging
|
||||
try {
|
||||
const files = fs.readdirSync(frontendDistPath);
|
||||
logger.info(`Frontend dist files: ${files.join(', ')}`);
|
||||
|
||||
const indexHtmlPath = path.join(frontendDistPath, 'index.html');
|
||||
if (fs.existsSync(indexHtmlPath)) {
|
||||
logger.info(`✅ index.html found at: ${indexHtmlPath}`);
|
||||
} else {
|
||||
logger.error(`❌ index.html NOT found at: ${indexHtmlPath}`);
|
||||
}
|
||||
|
||||
const assetsPath = path.join(frontendDistPath, 'assets');
|
||||
if (fs.existsSync(assetsPath)) {
|
||||
const assetFiles = fs.readdirSync(assetsPath);
|
||||
logger.info(`Frontend assets: ${assetFiles.length} files`);
|
||||
if (assetFiles.length > 0) {
|
||||
logger.info(`Sample assets: ${assetFiles.slice(0, 3).join(', ')}${assetFiles.length > 3 ? '...' : ''}`);
|
||||
}
|
||||
} else {
|
||||
logger.warn(`⚠️ Assets directory not found at: ${assetsPath}`);
|
||||
}
|
||||
} catch (err) {
|
||||
logger.warn(`Could not read frontend dist: ${err.message}`);
|
||||
logger.error(`❌ Could not read frontend dist: ${err.message}`);
|
||||
logger.error(`Error stack: ${err.stack}`);
|
||||
}
|
||||
|
||||
// Serve static files with proper headers for SPA
|
||||
@@ -223,41 +244,50 @@ app.use('/t', require('./routes/tracking.routes'));
|
||||
// SPA fallback: serve index.html for all non-API routes (must be after all routes)
|
||||
if (fs.existsSync(frontendDistPath)) {
|
||||
const indexHtmlPath = path.join(frontendDistPath, 'index.html');
|
||||
logger.info(`Frontend SPA fallback enabled: ${indexHtmlPath}`);
|
||||
|
||||
app.get('*', (req, res, next) => {
|
||||
// Skip API routes and tracking routes
|
||||
if (req.path.startsWith('/api') || req.path.startsWith('/t/') || req.path.startsWith('/health')) {
|
||||
return res.status(404).json({
|
||||
success: false,
|
||||
error: 'Endpoint not found',
|
||||
});
|
||||
}
|
||||
if (fs.existsSync(indexHtmlPath)) {
|
||||
logger.info(`✅ Frontend SPA fallback enabled: ${indexHtmlPath}`);
|
||||
|
||||
// Log SPA fallback
|
||||
logger.info(`SPA fallback: serving index.html for ${req.path}`);
|
||||
|
||||
// Serve frontend SPA
|
||||
if (fs.existsSync(indexHtmlPath)) {
|
||||
app.get('*', (req, res, next) => {
|
||||
// Skip API routes and tracking routes
|
||||
if (req.path.startsWith('/api') || req.path.startsWith('/t/') || req.path.startsWith('/health')) {
|
||||
return res.status(404).json({
|
||||
success: false,
|
||||
error: 'Endpoint not found',
|
||||
});
|
||||
}
|
||||
|
||||
// Log SPA fallback (only for non-asset requests to reduce noise)
|
||||
if (!req.path.startsWith('/assets/') && !req.path.match(/\.(js|css|png|jpg|jpeg|gif|svg|ico|woff|woff2|ttf|eot)$/)) {
|
||||
logger.info(`SPA fallback: serving index.html for ${req.path}`);
|
||||
}
|
||||
|
||||
// Serve frontend SPA
|
||||
res.sendFile(indexHtmlPath, (err) => {
|
||||
if (err) {
|
||||
logger.error(`Failed to send index.html: ${err.message}`);
|
||||
logger.error(`❌ Failed to send index.html: ${err.message}`);
|
||||
logger.error(`Error stack: ${err.stack}`);
|
||||
res.status(500).json({
|
||||
success: false,
|
||||
error: 'Failed to load frontend',
|
||||
});
|
||||
}
|
||||
});
|
||||
} else {
|
||||
logger.error(`index.html not found at: ${indexHtmlPath}`);
|
||||
res.status(500).json({
|
||||
});
|
||||
} else {
|
||||
logger.error(`❌ index.html not found at: ${indexHtmlPath}`);
|
||||
logger.error(`Frontend dist exists but index.html is missing!`);
|
||||
// 404 handler (if frontend not properly built)
|
||||
app.use((req, res) => {
|
||||
res.status(404).json({
|
||||
success: false,
|
||||
error: 'Frontend not found',
|
||||
error: 'Endpoint not found',
|
||||
});
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
} else {
|
||||
logger.warn(`Frontend dist not found at: ${frontendDistPath}`);
|
||||
logger.warn(`⚠️ Frontend dist not found at: ${frontendDistPath}`);
|
||||
logger.warn(`This is normal if using separate frontend/backend containers.`);
|
||||
// 404 handler (if frontend not built)
|
||||
app.use((req, res) => {
|
||||
res.status(404).json({
|
||||
|
||||
Reference in New Issue
Block a user