modified: backend/src/server.ts

This commit is contained in:
2025-12-18 13:24:41 +03:00
parent fac6e390ba
commit 26fbcd0d78

View File

@@ -4,7 +4,6 @@ import helmet from 'helmet';
// import rateLimit from 'express-rate-limit'; // import rateLimit from 'express-rate-limit';
import swaggerUi from 'swagger-ui-express'; import swaggerUi from 'swagger-ui-express';
import path from 'path'; import path from 'path';
import { createProxyMiddleware } from 'http-proxy-middleware';
import { config } from './config/environment'; import { config } from './config/environment';
import { initializeDatabase } from './config/database'; import { initializeDatabase } from './config/database';
import { generateDynamicSwagger } from './config/dynamicSwagger'; import { generateDynamicSwagger } from './config/dynamicSwagger';
@@ -117,19 +116,22 @@ if (config.nodeEnv === 'production') {
}); });
} else { } else {
// Development mode - proxy to Vite dev server for non-API routes // Development mode - proxy to Vite dev server for non-API routes
const viteProxy = createProxyMiddleware({ // Dynamic import to avoid requiring http-proxy-middleware in production
target: 'http://localhost:5173', import('http-proxy-middleware').then(({ createProxyMiddleware }) => {
changeOrigin: true, const viteProxy = createProxyMiddleware({
ws: true, // Enable WebSocket proxying for HMR target: 'http://localhost:5173',
}); changeOrigin: true,
ws: true, // Enable WebSocket proxying for HMR
});
app.use((req: Request, res: Response, next: any) => { app.use((req: Request, res: Response, next: any) => {
// If it's an API route or swagger, handle it normally // If it's an API route or swagger, handle it normally
if (req.path.startsWith('/api/') || req.path.startsWith('/api-docs') || req.path === '/health') { if (req.path.startsWith('/api/') || req.path.startsWith('/api-docs') || req.path === '/health') {
return next(); return next();
} }
// Otherwise, proxy to Vite dev server // Otherwise, proxy to Vite dev server
return viteProxy(req, res, next); return viteProxy(req, res, next);
});
}); });
// 404 handler for API routes only // 404 handler for API routes only