Create server.js
Browse files
server.js
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
const express = require("express");
|
| 2 |
+
const fetch = require("node-fetch");
|
| 3 |
+
const path = require("path");
|
| 4 |
+
|
| 5 |
+
const app = express();
|
| 6 |
+
|
| 7 |
+
const PORT = process.env.PORT || 7860;
|
| 8 |
+
const TARGET = process.env.TARGET_API_URL;
|
| 9 |
+
|
| 10 |
+
if (!TARGET) {
|
| 11 |
+
console.error("❌ 必须设置环境变量 TARGET_API_URL");
|
| 12 |
+
process.exit(1);
|
| 13 |
+
}
|
| 14 |
+
|
| 15 |
+
// 前端静态目录
|
| 16 |
+
app.use(express.static(path.join(__dirname, "public")));
|
| 17 |
+
|
| 18 |
+
// 代理接口
|
| 19 |
+
app.get("/api/data", async (req, res) => {
|
| 20 |
+
try {
|
| 21 |
+
const r = await fetch(TARGET, {
|
| 22 |
+
headers: {
|
| 23 |
+
"pragma": "no-cache"
|
| 24 |
+
}
|
| 25 |
+
});
|
| 26 |
+
|
| 27 |
+
const text = await r.text();
|
| 28 |
+
|
| 29 |
+
// 允许跨域
|
| 30 |
+
res.set("Access-Control-Allow-Origin", "*");
|
| 31 |
+
res.set("Content-Type", "application/json");
|
| 32 |
+
|
| 33 |
+
res.send(text);
|
| 34 |
+
} catch (e) {
|
| 35 |
+
res.status(500).json({ error: e.toString() });
|
| 36 |
+
}
|
| 37 |
+
});
|
| 38 |
+
|
| 39 |
+
app.listen(PORT, () => {
|
| 40 |
+
console.log("✅ Server running on port", PORT);
|
| 41 |
+
});
|