feat(remote): 添加 CORS 中间件支持文件跨域访问
This commit is contained in:
@@ -2,7 +2,7 @@ serverAddr = "146.56.248.142"
|
||||
serverPort = 7000
|
||||
auth.token = "wzw20040525"
|
||||
|
||||
log.to = "C:\\Users\\xuanchi\\Desktop\\remote\\logs\\frpc.log"
|
||||
log.to = "D:\\Xuanchi\\高斯泼溅\\XCDesktop\\remote\\logs\\frpc.log"
|
||||
log.level = "info"
|
||||
log.maxDays = 7
|
||||
|
||||
|
||||
27
remote/package-lock.json
generated
27
remote/package-lock.json
generated
@@ -13,6 +13,7 @@
|
||||
"bcryptjs": "^2.4.3",
|
||||
"config": "^3.3.12",
|
||||
"cookie-parser": "^1.4.7",
|
||||
"cors": "^2.8.6",
|
||||
"dotenv": "^17.3.1",
|
||||
"express": "^5.2.1",
|
||||
"h264-live-player": "^1.3.1",
|
||||
@@ -771,6 +772,23 @@
|
||||
"dev": true,
|
||||
"license": "MIT"
|
||||
},
|
||||
"node_modules/cors": {
|
||||
"version": "2.8.6",
|
||||
"resolved": "https://registry.npmjs.org/cors/-/cors-2.8.6.tgz",
|
||||
"integrity": "sha512-tJtZBBHA6vjIAaF6EnIaq6laBBP9aq/Y3ouVJjEfoHbRBcHBAHYcMh/w8LDrk2PvIMMq8gmopa5D4V8RmbrxGw==",
|
||||
"license": "MIT",
|
||||
"dependencies": {
|
||||
"object-assign": "^4",
|
||||
"vary": "^1"
|
||||
},
|
||||
"engines": {
|
||||
"node": ">= 0.10"
|
||||
},
|
||||
"funding": {
|
||||
"type": "opencollective",
|
||||
"url": "https://opencollective.com/express"
|
||||
}
|
||||
},
|
||||
"node_modules/debug": {
|
||||
"version": "4.4.3",
|
||||
"resolved": "https://registry.npmjs.org/debug/-/debug-4.4.3.tgz",
|
||||
@@ -1985,6 +2003,15 @@
|
||||
}
|
||||
}
|
||||
},
|
||||
"node_modules/object-assign": {
|
||||
"version": "4.1.1",
|
||||
"resolved": "https://registry.npmjs.org/object-assign/-/object-assign-4.1.1.tgz",
|
||||
"integrity": "sha512-rJgTQnkUnH1sFw8yT6VSU3zD3sWmu6sZhIseY8VX+GRu3P6F7Fu+JNDoXfklElbLJSnc3FUQHVe4cU5hj+BcUg==",
|
||||
"license": "MIT",
|
||||
"engines": {
|
||||
"node": ">=0.10.0"
|
||||
}
|
||||
},
|
||||
"node_modules/object-inspect": {
|
||||
"version": "1.13.4",
|
||||
"resolved": "https://registry.npmjs.org/object-inspect/-/object-inspect-1.13.4.tgz",
|
||||
|
||||
@@ -34,6 +34,7 @@
|
||||
"bcryptjs": "^2.4.3",
|
||||
"config": "^3.3.12",
|
||||
"cookie-parser": "^1.4.7",
|
||||
"cors": "^2.8.6",
|
||||
"dotenv": "^17.3.1",
|
||||
"express": "^5.2.1",
|
||||
"h264-live-player": "^1.3.1",
|
||||
|
||||
@@ -223,6 +223,17 @@ class App {
|
||||
const authMiddleware = require('../middlewares/auth');
|
||||
const routes = require('../routes');
|
||||
|
||||
// 简单的 CORS 中间件
|
||||
httpServer.use((req, res, next) => {
|
||||
res.header('Access-Control-Allow-Origin', '*');
|
||||
res.header('Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE, OPTIONS');
|
||||
res.header('Access-Control-Allow-Headers', 'Content-Type, Authorization');
|
||||
if (req.method === 'OPTIONS') {
|
||||
return res.sendStatus(200);
|
||||
}
|
||||
next();
|
||||
});
|
||||
|
||||
httpServer.use(cookieParser());
|
||||
httpServer.use(express.json());
|
||||
httpServer.use(express.urlencoded({ extended: true }));
|
||||
@@ -241,6 +252,11 @@ class App {
|
||||
});
|
||||
|
||||
httpServer.use(async (req, res, next) => {
|
||||
// 放行 CORS 预检请求
|
||||
if (req.method === 'OPTIONS') {
|
||||
return next();
|
||||
}
|
||||
|
||||
if (!authService.hasPassword()) {
|
||||
res.locals.authenticated = true;
|
||||
return next();
|
||||
|
||||
Reference in New Issue
Block a user