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