feat: 添加外网访问支持

This commit is contained in:
QZMusic
2026-06-04 15:15:17 +00:00
parent a4368eb232
commit a866d59053
3 changed files with 108 additions and 6 deletions

View File

@@ -9,6 +9,7 @@
"dev": "vite",
"build": "vue-tsc && vite build",
"preview": "vite preview --port 10096 --host",
"serve": "node server.cjs",
"deploy": "./deploy.sh",
"start": "./start.sh",
"install-app": "./install.sh",

69
server.cjs Normal file
View File

@@ -0,0 +1,69 @@
/**
* QZMusic Web 静态文件服务器
* 外网访问支持
*/
const http = require('http');
const fs = require('fs');
const path = require('path');
const PORT = 10096;
const ROOT = path.join(__dirname, 'dist');
const mimeTypes = {
'.html': 'text/html',
'.js': 'text/javascript',
'.css': 'text/css',
'.json': 'application/json',
'.png': 'image/png',
'.jpg': 'image/jpg',
'.gif': 'image/gif',
'.svg': 'image/svg+xml',
'.ico': 'image/x-icon',
'.ttf': 'font/ttf',
'.woff': 'font/woff',
'.woff2': 'font/woff2'
};
const server = http.createServer((req, res) => {
let filePath = path.join(ROOT, req.url === '/' ? 'index.html' : req.url);
const extname = String(path.extname(filePath)).toLowerCase();
const contentType = mimeTypes[extname] || 'application/octet-stream';
if (!extname && !fs.existsSync(filePath)) {
filePath = path.join(ROOT, 'index.html');
}
fs.readFile(filePath, (error, content) => {
if (error) {
if (error.code === 'ENOENT') {
fs.readFile(path.join(ROOT, 'index.html'), (err, html) => {
res.writeHead(200, { 'Content-Type': 'text/html' });
res.end(html, 'utf-8');
});
} else {
res.writeHead(500);
res.end('Sorry, check with the site admin for error: ' + error.code + ' ..\n');
}
} else {
res.writeHead(200, { 'Content-Type': contentType });
res.end(content, 'utf-8');
}
});
});
server.listen(PORT, '0.0.0.0', () => {
console.log(`
╔═══════════════════════════════════════════════════════════════════════╗
║ ║
║ QZMusic Web Server 启动成功! ║
║ ║
║ 本地访问http://localhost:${PORT}
║ 局域网访问http://[你的IP]:${PORT}
║ ║
║ 外网访问:请配置端口转发/公网IP访问 ║
║ ║
╚═══════════════════════════════════════════════════════════════════════╝
`);
});

View File

@@ -18,10 +18,42 @@ if [ ! -d "node_modules" ]; then
fi
fi
echo "🚀 正在启动开发服务器..."
echo "🌐 访问地址: http://localhost:10096"
echo ""
echo "按 Ctrl+C 停止服务"
echo ""
# 选择启动模式
echo "请选择启动模式:"
echo " 1) 开发模式(带热更新)"
echo " 2) 生产模式(静态文件服务"
read -p "请输入选项 (1/2默认 2): " choice
npm run dev
case ${choice:-2} in
1)
echo ""
echo "🚀 正在启动开发服务器..."
echo "🌐 访问地址: http://localhost:10096"
echo ""
echo "按 Ctrl+C 停止服务器"
echo ""
npm run dev
;;
2)
# 检查是否已构建
if [ ! -d "dist" ]; then
echo "📦 未找到构建文件,正在构建..."
npm run build
if [ $? -ne 0 ]; then
echo "❌ 构建失败!"
exit 1
fi
fi
echo ""
echo "🚀 正在启动生产服务器..."
echo "🌐 访问地址: http://[你的IP]:10096"
echo ""
echo "按 Ctrl+C 停止服务器"
echo ""
npm run serve
;;
*)
echo "❌ 无效选项!"
exit 1
;;
esac