This commit is contained in:
Dmitry 2026-01-05 01:11:25 +03:00
parent 3b879f80d4
commit 178a2ba23b
2 changed files with 28 additions and 1 deletions

View file

@ -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 и т.д.)
## 🎨 Особенности

View file

@ -7,9 +7,29 @@ async function bootstrap() {
const app = await NestFactory.create(AppModule);
const configService = app.get(ConfigService);
// Поддержка нескольких origins через запятую или одного origin
const corsOrigin = configService.get<string>('CORS_ORIGIN') || 'http://localhost:5173';
const allowedOrigins = corsOrigin.split(',').map(origin => origin.trim());
app.enableCors({
origin: configService.get<string>('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<string>('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();