config:优化WebSocket远程连接的CORS配置

- 明确指定允许的域名列表,包括生产环境域名
- 添加Vite开发服务器端口支持
- 完善CORS方法和头部配置,确保WebSocket握手正常
- 支持xinghangee.icu子域名的通配符匹配

修复远程域名WebSocket连接问题的核心配置
This commit is contained in:
moyin
2026-01-05 11:13:43 +08:00
parent 70c020a97c
commit 6002f53cbc

View File

@@ -40,10 +40,17 @@ async function bootstrap() {
logger: ['error', 'warn', 'log'], logger: ['error', 'warn', 'log'],
}); });
// 允许前端后台如Vite/React跨域访问 // 允许前端后台如Vite/React跨域访问包括WebSocket
app.enableCors({ app.enableCors({
origin: true, origin: [
'http://localhost:3000',
'http://localhost:5173', // Vite默认端口
'https://whaletownend.xinghangee.icu',
/^https:\/\/.*\.xinghangee\.icu$/
],
credentials: true, credentials: true,
methods: ['GET', 'POST', 'PUT', 'DELETE', 'OPTIONS'],
allowedHeaders: ['Content-Type', 'Authorization', 'X-Requested-With'],
}); });
// 全局启用校验管道(核心配置) // 全局启用校验管道(核心配置)