-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproxy-server.js
More file actions
33 lines (29 loc) · 1.08 KB
/
Copy pathproxy-server.js
File metadata and controls
33 lines (29 loc) · 1.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
import express from 'express';
import { createProxyMiddleware } from 'http-proxy-middleware';
import cors from 'cors';
const app = express();
const PORT = 3001;
// 启用CORS
app.use(cors({
origin: ['http://localhost:8080', 'http://localhost:8081', 'http://localhost:8082', 'http://localhost:8083', 'http://localhost:8084', 'http://localhost:8085', 'http://localhost:8086', 'http://localhost:8087', 'http://localhost:8088', 'http://localhost:8089'],
credentials: true
}));
// OpenAI API代理
app.use('/api/openai', createProxyMiddleware({
target: 'https://api.openai.com/v1',
changeOrigin: true,
pathRewrite: {
'^/api/openai': '', // 移除 /api/openai 前缀
},
onProxyReq: (proxyReq, req, res) => {
console.log(`Proxying request: ${req.method} ${req.url}`);
},
onError: (err, req, res) => {
console.error('Proxy error:', err);
res.status(500).json({ error: 'Proxy error' });
}
}));
app.listen(PORT, () => {
console.log(`CORS Proxy server running on http://localhost:${PORT}`);
console.log(`OpenAI API proxied at http://localhost:${PORT}/api/openai`);
});