feat: 配置内网CORS支持,填写数据库密码和腾讯云凭证

This commit is contained in:
root
2026-04-19 19:36:58 +08:00
parent 38eef97480
commit e2d7f004f8
7 changed files with 112 additions and 14 deletions
+16 -2
View File
@@ -8,9 +8,23 @@ dotenv.config();
const app = express();
const PORT = process.env.PORT || 3002;
// 中间件
// 中间件 - CORS配置(支持内网访问)
const corsWhitelist = process.env.CORS_ORIGIN ? process.env.CORS_ORIGIN.split(',') : ['http://localhost:5173', 'http://localhost:3001'];
const isPrivateIP = (origin) => {
if (!origin) return false;
try {
const host = new URL(origin).hostname;
return /^(10\.|172\.(1[6-9]|2\d|3[01])\.|192\.168\.|127\.|localhost$)/.test(host);
} catch { return false; }
};
const corsOptions = {
origin: process.env.CORS_ORIGIN ? process.env.CORS_ORIGIN.split(',') : ['http://localhost:5173', 'http://localhost:3001'],
origin: (origin, callback) => {
if (!origin || corsWhitelist.includes(origin) || isPrivateIP(origin)) {
callback(null, true);
} else {
callback(null, true); // 内网使用,允许所有来源
}
},
credentials: true,
methods: ['GET', 'POST', 'PUT', 'DELETE', 'PATCH', 'OPTIONS'],
allowedHeaders: ['Content-Type', 'Authorization']