Skip to content

Commit

Permalink
fix: 修复CORS问题
Browse files Browse the repository at this point in the history
  • Loading branch information
pysio2007 committed Jan 1, 2025
1 parent f1f9708 commit 4858144
Showing 1 changed file with 12 additions and 3 deletions.
15 changes: 12 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,10 +93,19 @@ function parseAnsiColors(text: string): string {
return result.join('');
}

app.use((req: Request, res: Response, next: NextFunction) => {
// 替换现有的 CORS 中间件
app.use(function corsMiddleware(req: Request, res: Response, next: NextFunction) {
res.header('Access-Control-Allow-Origin', '*');
res.header('Access-Control-Allow-Headers', 'Content-Type,Authorization');
res.header('Access-Control-Allow-Methods', 'GET,PUT,POST');
res.header('Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE, OPTIONS');
res.header('Access-Control-Allow-Headers', 'Origin, X-Requested-With, Content-Type, Accept, Authorization');
res.header('Access-Control-Max-Age', '86400'); // 24小时缓存预检请求结果

// 处理 OPTIONS 预检请求
if (req.method === 'OPTIONS') {
res.status(200).end();
return;
}

next();
});

Expand Down

0 comments on commit 4858144

Please sign in to comment.