PozoApp.dev-optimized-and-D.../server.js

105 lines
2.7 KiB
JavaScript
Raw Normal View History

// server.js - IIS Node.js Proxy for React + .NET Backend
const express = require('express');
const path = require('path');
const { createProxyMiddleware } = require('http-proxy-middleware');
const app = express();
const PORT = process.env.PORT || 3001;
const isProduction = process.env.NODE_ENV === 'production';
// Configuration
const config = {
distPath: path.join(__dirname, 'Update Common - Vignesh', 'dist'),
dotnetApiUrl: process.env.DOTNET_API_URL || 'http://localhost:5000'
};
console.log('🚀 Starting PozoApp Server...');
console.log('📦 Environment:', isProduction ? 'Production' : 'Development');
console.log('📁 Serving React from:', config.distPath);
// Middleware - Serve static files from React build
app.use(express.static(config.distPath, {
maxAge: isProduction ? '1d' : 0,
etag: true
}));
// Proxy .NET API calls
app.use('/api', createProxyMiddleware({
target: config.dotnetApiUrl,
changeOrigin: true,
pathRewrite: {
'^/api': '/api' // Keep /api prefix
},
onProxyReq: (proxyReq, req, res) => {
console.log('🔗 API Proxy:', req.method, req.url, '→', config.dotnetApiUrl);
},
onError: (err, req, res) => {
console.error('❌ Proxy error:', err.message);
res.status(500).send('Backend API unavailable');
}
}));
// Health check endpoint
app.get('/health', (req, res) => {
res.json({
status: 'OK',
environment: process.env.NODE_ENV,
timestamp: new Date().toISOString()
});
});
// React SPA - Handle all frontend routes
// Clean URLs without /home/ prefix
const reactRoutes = [
'/',
'/blog',
'/pricing',
'/contact-us',
'/signin',
'/faq',
'/testimonials',
'/privacy-policy',
'/cookie-policy',
'/about-us',
'/live-session',
'/adminpanel',
'/landing-page/*',
'/setting/*',
'/:appName' // Dynamic app routes (bakery, restaurant, etc.)
];
// Serve index.html for all non-API routes
app.get('*', (req, res) => {
const indexPath = path.join(config.distPath, 'index.html');
// Log for debugging
console.log('📄 Serving React SPA for:', req.url);
res.sendFile(indexPath, (err) => {
if (err) {
console.error('❌ Error serving index.html:', err);
res.status(500).send('Error loading application');
}
});
});
// Error handling
app.use((err, req, res, next) => {
console.error('❌ Server Error:', err);
res.status(500).send('Internal Server Error');
});
// Start server
app.listen(PORT, () => {
console.log(`✅ PozoApp Server listening on port ${PORT}`);
console.log(`🌐 Access at: http://localhost:${PORT}`);
console.log(`📊 Clean URLs enabled (no /home/ prefix)`);
});
// Graceful shutdown
process.on('SIGTERM', () => {
console.log('🛑 SIGTERM received, shutting down gracefully...');
process.exit(0);
});