// 校跑侠 · Web 后端入口 // 提供:客户端 API(/api)、管理端 API(/api/admin)、管理后台静态页(/admin) import express from 'express' import cors from 'cors' import path from 'node:path' import { fileURLToPath } from 'node:url' import { initStore } from './src/store.js' import clientRoutes from './src/routes/client.js' import adminRoutes from './src/routes/admin.js' const __dirname = path.dirname(fileURLToPath(import.meta.url)) const PORT = process.env.PORT || 8090 const HOST = '0.0.0.0' initStore() const app = express() app.use(cors()) // 允许 H5(8088) 跨域调用 app.use(express.json()) // 请求日志(简单) app.use((req, res, next) => { const t = new Date().toISOString().slice(11, 19) console.log(`[${t}] ${req.method} ${req.url}`) next() }) // 健康检查 app.get('/api/health', (req, res) => res.json({ code: 0, msg: 'ok', data: { status: 'up', time: Date.now() } }) ) // 业务路由 app.use('/api/admin', adminRoutes) app.use('/api', clientRoutes) // 管理后台静态资源 app.use('/admin', express.static(path.join(__dirname, 'public', 'admin'))) // 根路径导航 app.get('/', (req, res) => { res.type('html').send( `

校跑侠 · Web 后端

` ) }) // 404 app.use((req, res) => { res.status(404).json({ code: 404, msg: '接口不存在', data: null }) }) app.listen(PORT, HOST, () => { console.log(`\n校跑侠后端已启动`) console.log(` 本地 : http://localhost:${PORT}`) console.log(` 管理台 : http://localhost:${PORT}/admin/`) console.log(` API : http://localhost:${PORT}/api\n`) })