-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
53 lines (44 loc) · 1.27 KB
/
server.js
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
const http = require('http')
const httpProxy = require('http-proxy')
require('dotenv').config()
const GPT_API_BACKEND = process.env.GPT_API_BACKEND
// 创建代理服务器
const proxy = httpProxy.createProxyServer({
changeOrigin: true,
ws: true, // 支持 WebSocket 代理
})
// 处理错误
proxy.on('error', (err, req, res) => {
console.error('Proxy error:', err)
if (!res.headersSent) {
res.writeHead(500, { 'Content-Type': 'text/plain' })
}
res.end('Proxy encountered an error')
})
// 创建 HTTP 服务器
const server = http.createServer((req, res) => {
const target = GPT_API_BACKEND // 目标服务器
console.log(`Proxying request: ${req.method} ${req.url} -> ${target}`)
// 设置 CORS 头部,允许跨域
res.setHeader('Access-Control-Allow-Origin', '*')
res.setHeader(
'Access-Control-Allow-Methods',
'GET, POST, PUT, DELETE, OPTIONS',
)
res.setHeader('Access-Control-Allow-Headers', 'Content-Type, Authorization')
// 处理预检请求
if (req.method === 'OPTIONS') {
res.writeHead(204)
res.end()
return
}
proxy.web(req, res, {
target: target,
selfHandleResponse: false,
})
})
// 监听端口
const PORT = 3000
server.listen(PORT, () => {
console.log(`Proxy server is running on http://localhost:${PORT}`)
})