File size: 887 Bytes
e167dd8 | 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 | const express = require("express");
const fetch = require("node-fetch");
const path = require("path");
const app = express();
const PORT = process.env.PORT || 7860;
const TARGET = process.env.TARGET_API_URL;
if (!TARGET) {
console.error("❌ 必须设置环境变量 TARGET_API_URL");
process.exit(1);
}
// 前端静态目录
app.use(express.static(path.join(__dirname, "public")));
// 代理接口
app.get("/api/data", async (req, res) => {
try {
const r = await fetch(TARGET, {
headers: {
"pragma": "no-cache"
}
});
const text = await r.text();
// 允许跨域
res.set("Access-Control-Allow-Origin", "*");
res.set("Content-Type", "application/json");
res.send(text);
} catch (e) {
res.status(500).json({ error: e.toString() });
}
});
app.listen(PORT, () => {
console.log("✅ Server running on port", PORT);
});
|