feat(remote): 添加 CORS 中间件支持文件跨域访问
This commit is contained in:
@@ -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