-
Notifications
You must be signed in to change notification settings - Fork 73
/
Copy pathindex.js
46 lines (38 loc) · 1.44 KB
/
index.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
const express = require('express');
const axios = require('axios');
const app = express();
const PORT = process.env.PORT || 3000;
const targetURLs = process.env.TARGET_URLS ? process.env.TARGET_URLS.split(',') : ['https://deeplx.vercel.app'];
app.use(express.json());
app.use(express.text());
app.all('*', async (req, res) => {
// 创建请求配置,考虑设置请求超时
const createRequestConfig = (targetURL) => ({
method: req.method,
url: targetURL + req.path,
data: req.method !== 'GET' ? req.body : null,
timeout: 5000, // 设置5秒超时
});
// 封装请求以捕获并处理单个请求的错误
const makeRequest = async (config) => {
try {
return await axios(config);
} catch (error) {
return Promise.reject(`请求到 ${config.url} 失败: ${error.message}`);
}
};
try {
const responses = await Promise.any(targetURLs.map(targetURL => makeRequest(createRequestConfig(targetURL))));
// 响应成功,设置响应头并返回数据
res.set({
'Access-Control-Allow-Origin': '*',
'Content-Type': 'application/json', // 根据需要调整Content-Type
}).status(responses.status).send(responses.data);
} catch (error) {
// 所有请求都失败了
res.status(500);
}
});
app.listen(PORT, () => {
console.log(`Server is running on http://localhost:${PORT}`);
});