diff --git a/package.json b/package.json index 26ad77d..2bb9211 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/server.cjs b/server.cjs new file mode 100644 index 0000000..decb2f4 --- /dev/null +++ b/server.cjs @@ -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访问 ║ +║ ║ +╚═══════════════════════════════════════════════════════════════════════╝ + `); +}); diff --git a/start.sh b/start.sh index 7dd6d7c..adc56d1 100755 --- a/start.sh +++ b/start.sh @@ -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