#!/usr/bin/env node /** * Uniapp Material Design项目开发服务器 * 绑定到所有IP地址,支持公网访问 */ const express = require('express'); const cors = require('cors'); const path = require('path'); const fs = require('fs'); // 创建Express应用 const app = express(); const PORT = process.env.PORT || 8000; const HOST = '0.0.0.0'; // 绑定到所有IP // 启用CORS app.use(cors()); // 解析JSON请求体 app.use(express.json()); app.use(express.urlencoded({ extended: true })); // 静态文件服务 app.use(express.static(path.join(__dirname))); // 模拟Uniapp的页面路由 app.get('/pages/:page/:page', (req, res) => { const pagePath = path.join(__dirname, 'pages', req.params.page, `${req.params.page}.vue`); if (fs.existsSync(pagePath)) { // 如果是Vue文件,返回一个简单的HTML页面 res.send(` 玩Android - ${req.params.page}

玩Android Material Design

开发服务器预览

📱
${req.params.page}页面
这是Uniapp Material Design项目的${req.params.page}页面预览
服务器信息
页面路径: /pages/${req.params.page}/${req.params.page}.vue
服务器地址: ${req.protocol}://${req.get('host')}
公网IP: 129.211.173.87:${PORT}
访问时间: ${new Date().toLocaleString()}
返回首页
`); } else { res.status(404).send('页面不存在'); } }); // 首页路由 app.get('/', (req, res) => { res.send(` 玩Android Material Design - 首页
🏠
首页功能
Banner轮播、文章列表、搜索热词、下拉刷新、上拉加载
🎨
Material Design
遵循Material Design 3规范,紫色主色系统,完整设计语言
📱
底部导航
5个Tab导航,轮廓/填充双状态图标,平滑切换动画
🔌
API集成
集成玩Android开放API,统一网络请求和错误处理
服务器信息
服务器地址:
${req.protocol}://${req.get('host')}
公网IP:
129.211.173.87:${PORT}
项目路径:
/home/ubuntu/wanandroid/uniapp_android
启动时间:
${new Date().toLocaleString()}
Node版本:
${process.version}
平台:
${process.platform} ${process.arch}
🏠
首页
📦
项目
📱
公众号
🌳
体系
👤
我的
`); }); // API模拟 - 玩Android API app.get('/api/banner/json', (req, res) => { res.json({ data: [ { desc: "Material Design Banner 1", id: 1, imagePath: "https://via.placeholder.com/300x150/6200EE/ffffff?text=Banner1", isVisible: 1, order: 0, title: "玩Android Material Design", type: 0, url: "https://www.wanandroid.com" }, { desc: "Material Design Banner 2", id: 2, imagePath: "https://via.placeholder.com/300x150/03DAC6/ffffff?text=Banner2", isVisible: 1, order: 1, title: "Uniapp开发框架", type: 0, url: "https://uniapp.dcloud.io" }, { desc: "Material Design Banner 3", id: 3, imagePath: "https://via.placeholder.com/300x150/FF9800/ffffff?text=Banner3", isVisible: 1, order: 2, title: "Android应用开发", type: 0, url: "https://developer.android.com" } ], errorCode: 0, errorMsg: "" }); }); app.get('/api/hotkey/json', (req, res) => { res.json({ data: [ { id: 1, link: "", name: "Material Design", order: 1, visible: 1 }, { id: 2, link: "", name: "Uniapp", order: 2, visible: 1 }, { id: 3, link: "", name: "Android", order: 3, visible: 1 }, { id: 4, link: "", name: "Vue 3", order: 4, visible: 1 }, { id: 5, link: "", name: "玩Android", order: 5, visible: 1 }, { id: 6, link: "", name: "API开发", order: 6, visible: 1 } ], errorCode: 0, errorMsg: "" }); }); app.get('/api/article/list/0/json', (req, res) => { res.json({ data: { curPage: 0, datas: [ { id: 1, title: "玩Android Material Design客户端发布", link: "https://www.wanandroid.com", author: "Material Design", shareUser: "", chapterName: "前端开发", superChapterName: "移动开发", niceDate: "刚刚", publishTime: Date.now(), collect: false, zan: 42, fresh: true, tags: [] }, { id: 2, title: "Uniapp框架Material Design实践指南", link: "https://uniapp.dcloud.io", author: "Uniapp团队", shareUser: "", chapterName: "Uniapp", superChapterName: "跨端开发", niceDate: "1小时前", publishTime: Date.now() - 3600000, collect: true, zan: 28, fresh: false, tags: [] }, { id: 3, title: "Android Material Design 3新特性详解", link: "https://developer.android.com", author: "Google", shareUser: "", chapterName: "Android", superChapterName: "移动开发", niceDate: "3小时前", publishTime: Date.now() - 10800000, collect: false, zan: 56, fresh: false, tags: [] }, { id: 4, title: "玩Android API接口完整文档", link: "https://www.wanandroid.com/blog/show/2", author: "玩Android", shareUser: "", chapterName: "API文档", superChapterName: "开发资源", niceDate: "1天前", publishTime: Date.now() - 86400000, collect: true, zan: 89, fresh: false, tags: [] } ], offset: 0, over: false, pageCount: 5, size: 20, total: 20 }, errorCode: 0, errorMsg: "" }); }); // 健康检查 app.get('/health', (req, res) => { res.json({ status: 'healthy', timestamp: new Date().toISOString(), service: 'wanandroid-material-server', version: '1.0.0', uptime: process.uptime() }); }); // 404处理 app.use((req, res) => { res.status(404).send(` 404 - 页面未找到
🔍
404 - 页面未找到
请求的页面不存在
${req.originalUrl}
返回首页
`); }); // 启动服务器 app.listen(PORT, HOST, () => { console.log(` ============================================== 玩Android Material Design开发服务器 ============================================== ✅ 服务器启动成功! 📱 本地访问: http://localhost:${PORT} http://127.0.0.1:${PORT} 🌐 公网访问: http://129.211.173.87:${PORT} 📁 项目信息: 项目路径: /home/ubuntu/wanandroid/uniapp_android 服务器IP: ${HOST} 服务端口: ${PORT} Node版本: ${process.version} 🚀 可用路由: / - 首页预览 /material_preview.html - Material Design预览 /api/banner/json - Banner数据API /api/hotkey/json - 搜索热词API /api/article/list/0/json - 文章列表API /health - 健康检查 ⏰ 启动时间: ${new Date().toLocaleString()} ============================================== `); // 打印网络信息 const os = require('os'); const networkInterfaces = os.networkInterfaces(); console.log('📡 网络接口信息:'); Object.keys(networkInterfaces).forEach(interfaceName => { networkInterfaces[interfaceName].forEach(interface => { if (interface.family === 'IPv4' && !interface.internal) { console.log(` ${interfaceName}: ${interface.address}`); } }); }); console.log('==============================================\n'); }); // 优雅关闭 process.on('SIGINT', () => { console.log('\n🛑 收到关闭信号,正在停止服务器...'); process.exit(0); }); process.on('SIGTERM', () => { console.log('\n🛑 收到终止信号,正在停止服务器...'); process.exit(0); });