diff --git a/README.md b/README.md index 5bb23a3..6dc77f3 100644 --- a/README.md +++ b/README.md @@ -178,6 +178,11 @@ Frontend: http://localhost:5173 - Frontend: `https://example.com` - Backend: `https://api.example.com` - `CORS_ORIGIN` должен быть: `https://example.com` + + **Поддержка нескольких origins:** Можно указать несколько доменов через запятую: + ``` + CORS_ORIGIN=https://party-games.online,https://www.party-games.online,http://localhost:5173 + ``` ### Frontend переменные: - `VITE_API_URL` - URL backend API (по умолчанию http://localhost:3000) @@ -275,6 +280,7 @@ npm run preview # Preview build - [Backend README](backend/README.md) - детальная документация backend - [PLAN.md](PLAN.md) - полный план разработки - [API Documentation](backend/README.md#-api-endpoints) - REST и WebSocket API +- [TROUBLESHOOTING.md](TROUBLESHOOTING.md) - решение проблем с деплоем (502 ошибки, CORS и т.д.) ## 🎨 Особенности diff --git a/backend/src/main.ts b/backend/src/main.ts index 285ccc6..1d216c0 100644 --- a/backend/src/main.ts +++ b/backend/src/main.ts @@ -7,9 +7,29 @@ async function bootstrap() { const app = await NestFactory.create(AppModule); const configService = app.get(ConfigService); + // Поддержка нескольких origins через запятую или одного origin + const corsOrigin = configService.get('CORS_ORIGIN') || 'http://localhost:5173'; + const allowedOrigins = corsOrigin.split(',').map(origin => origin.trim()); + app.enableCors({ - origin: configService.get('CORS_ORIGIN') || 'http://localhost:5173', + origin: (origin, callback) => { + // Разрешаем запросы без origin (например, Postman, мобильные приложения) + if (!origin) { + return callback(null, true); + } + + // Проверяем, есть ли origin в списке разрешенных + if (allowedOrigins.includes(origin)) { + return callback(null, true); + } + + // Логируем отклоненные запросы для отладки + console.warn(`CORS: Blocked origin: ${origin}. Allowed origins: ${allowedOrigins.join(', ')}`); + return callback(new Error('Not allowed by CORS')); + }, credentials: true, + methods: ['GET', 'POST', 'PUT', 'DELETE', 'PATCH', 'OPTIONS'], + allowedHeaders: ['Content-Type', 'Authorization'], }); app.useGlobalPipes(new ValidationPipe({ @@ -21,5 +41,6 @@ async function bootstrap() { const host = configService.get('HOST') || '0.0.0.0'; await app.listen(port, host); console.log(`Backend running on http://${host}:${port}`); + console.log(`CORS allowed origins: ${allowedOrigins.join(', ')}`); } bootstrap();