feat: 配置内网CORS支持,填写数据库密码和腾讯云凭证
This commit is contained in:
+16
-2
@@ -15,9 +15,23 @@ const imageFormats = ['jpg', 'jpeg', 'png', 'gif', 'webp', 'bmp', 'svg'];
|
||||
const app = express();
|
||||
const PORT = process.argv[2] || process.env.PORT || 3000;
|
||||
|
||||
// 中间件
|
||||
// 中间件 - 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']
|
||||
|
||||
+16
-2
@@ -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']
|
||||
|
||||
+16
-2
@@ -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']
|
||||
|
||||
+16
-2
@@ -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']
|
||||
|
||||
@@ -28,9 +28,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']
|
||||
|
||||
@@ -9,9 +9,23 @@ const financeRouter = require('./finance-api');
|
||||
const app = express();
|
||||
const PORT = process.env.PORT || 5000;
|
||||
|
||||
// 中间件
|
||||
// 中间件 - 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']
|
||||
|
||||
@@ -8,9 +8,23 @@ const db = require('./db');
|
||||
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']
|
||||
|
||||
Reference in New Issue
Block a user