mirror of
https://github.com/setube/ogame-vue-ts.git
synced 2026-05-12 07:55:11 +08:00
- 移除对 'open' 包的依赖,改用 Node.js 内置模块实现自动打开浏览器 - 新增 openUrl 函数,支持 macOS、Windows 和 Linux 系统 - 更新网络接口遍历逻辑中的变量命名以提高可读性 - 静态资源处理中间件改为使用 Bun.file API 并内嵌到可执行文件中 - 优化控制台输出信息,增强用户体验和提示清晰度 - 调整服务器监听地址为 0.0.0.0,并移除 trust proxy 设置 - 修改获取局域网 IP 的函数名称和注释结构使其更加明确 - 删除 package.json 和 lock 文件中不再使用的依赖项及相关条目 - 更新 GitHub Actions 工作流配置以适配新的编译和打包方式 - 在 CI 流程中启用代码压缩选项以减小最终二进制文件体积
64 lines
2.2 KiB
JavaScript
64 lines
2.2 KiB
JavaScript
const express = require('express');
|
|
const path = require('path');
|
|
const { exec } = require('child_process');
|
|
const os = require('os');
|
|
|
|
const app = express();
|
|
|
|
// 1. 跨平台自动打开浏览器函数
|
|
function openUrl(url) {
|
|
const start = process.platform === 'darwin' ? 'open' : process.platform === 'win32' ? 'start ""' : 'xdg-open';
|
|
exec(`${start} "${url}"`);
|
|
}
|
|
|
|
// 2. 获取局域网 IP
|
|
function getLocalIp() {
|
|
const interfaces = os.networkInterfaces();
|
|
for (let devName in interfaces) {
|
|
let iface = interfaces[devName];
|
|
for (let i = 0; i < iface.length; i++) {
|
|
let alias = iface[i];
|
|
if (alias.family === 'IPv4' && alias.address !== '127.0.0.1' && !alias.internal) {
|
|
return alias.address;
|
|
}
|
|
}
|
|
}
|
|
return 'localhost';
|
|
}
|
|
|
|
// 3. 核心:静态资源拦截器(实现单文件嵌入的关键)
|
|
app.get('*', async (req, res) => {
|
|
// 处理请求路径,默认为 index.html
|
|
let reqPath = req.path === '/' ? '/index.html' : req.path;
|
|
|
|
// 这里的路径必须在构建时能找到对应的 docs 目录
|
|
// Bun 编译时会自动将 Bun.file 引用的静态资源打包进去
|
|
const filePath = path.join(__dirname, "docs", reqPath);
|
|
const file = Bun.file(filePath);
|
|
|
|
if (await file.exists()) {
|
|
res.type(file.type);
|
|
res.send(Buffer.from(await file.arrayBuffer()));
|
|
} else {
|
|
// Vue History 模式支持:找不到的文件指向 index.html
|
|
const indexFile = Bun.file(path.join(__dirname, "docs", "index.html"));
|
|
res.type('text/html');
|
|
res.send(Buffer.from(await indexFile.arrayBuffer()));
|
|
}
|
|
});
|
|
|
|
// 4. 启动服务器(随机端口)
|
|
const server = app.listen(0, '0.0.0.0', () => {
|
|
const { port } = server.address();
|
|
const url = `http://localhost:${port}`;
|
|
const lanUrl = `http://${getLocalIp()}:${port}`;
|
|
|
|
console.log("-----------------------------------");
|
|
console.log(`🚀 OGame 程序已启动!`);
|
|
console.log(`🔗 本地访问: ${url}`);
|
|
console.log(`🌐 局域网访问: ${lanUrl}`);
|
|
console.log("-----------------------------------");
|
|
console.log("提示: 关闭此窗口将停止服务。");
|
|
|
|
openUrl(url);
|
|
}); |