Files
ogame-vue-ts/server.js
coolxitech b77ae14d5c chore(deps): 添加 express 和 open 依赖项
- 在 package.json 中添加 express 版本 5.2.1
- 在 package.json 中添加 open 版本 11.0.0
- 更新 pnpm-lock.yaml 文件以包含新的依赖项及其子依赖项
- 添加与新依赖项相关的中间件和工具库
- 确保所有新增依赖项的版本兼容性
2025-12-13 13:16:11 +08:00

44 lines
1.4 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
const express = require('express');
const path = require('node:path');
const open = require('open');
const os = require('node:os');
const app = express();
const HOST = '0.0.0.0';
app.set('trust proxy', true);
// 指向 Vue 构建后的 dist 目录
app.use(express.static(path.join(process.cwd(), 'dist')));
const 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];
if (alias.family === 'IPv4' && alias.address !== '127.0.0.1' && !alias.internal) {
return alias.address;
}
}
}
return 'localhost';
}
const server = app.listen(0, HOST, async () => {
const { port } = server.address();
const url = `http://localhost:${port}`;
console.log('-----------------------------------');
console.log(`🚀 服务器已成功启动!`);
console.log(`🔗 本地地址: ${url}`);
console.log(`🌐 默认允许局域网访问http://${getLocalIp()}:${port}`);
console.log('-----------------------------------');
console.log('提示: 关闭此控制台窗口将停止服务。');
// 3. 自动打开浏览器
try {
await open(url);
} catch (err) {
console.error('无法自动打开浏览器:', err);
}
});