feat(remote): 添加 CORS 中间件支持文件跨域访问

This commit is contained in:
2026-03-09 17:27:47 +08:00
parent 92088e9c8a
commit 49bf8a97d2
22 changed files with 467 additions and 62 deletions

View File

@@ -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();