const { app, BrowserWindow } = require("electron"); const path = require("path"); const http = require("http"); const httpProxy = require("http-proxy"); // 需要安装:npm install http-proxy // 创建代理服务器 const proxyServer = httpProxy.createProxyServer({}); // 定义目标服务 const proxyTargets = { "/live-digital-avatar-manage": "http://192.168.10.25:8080/live-digital-avatar-manage", "/asr": "http://192.168.10.138:8090", "/tts": "http://192.168.10.113:9880" }; // 创建本地 HTTP 服务器 const server = http.createServer((req, res) => { // 根据请求路径匹配目标服务 for (const pathPrefix in proxyTargets) { if (req.url.startsWith(pathPrefix)) { const target = proxyTargets[pathPrefix]; // 重写路径(移除前缀) req.url = req.url.replace(pathPrefix, ""); // 转发请求 proxyServer.web(req, res, { target }); return; } } res.statusCode = 404; res.end("Not Found"); }); // 启动服务器 server.listen(3000, () => { console.log("Proxy server running on port 3000"); }); function createWindow() { const win = new BrowserWindow({ width: 800, height: 600, webPreferences: { preload: path.join(__dirname, "preload.js"), // 使用计算后的 __dirname nodeIntegration: true, contextIsolation: false } }); // 加载应用 win.loadFile("dist/index.html"); } app.whenReady().then(createWindow);