const express = require('express'); const cors = require('cors'); const path = require('path'); const ordersRouter = require('./routes/orders'); const app = express(); const PORT = process.env.PORT || 3000; app.use(cors()); app.use(express.json({ limit: '50mb' })); // API 路由 app.use('/api/orders', ordersRouter); // 健康检查 app.get('/api/health', (req, res) => { res.json({ status: 'ok', timestamp: new Date().toISOString() }); }); // 生产环境:托管前端静态文件 const distPath = path.join(__dirname, '..', 'web', 'dist'); app.use(express.static(distPath)); // SPA 兜底:所有非API请求返回 index.html app.get('*', (req, res, next) => { if (req.path.startsWith('/api')) return next(); res.sendFile(path.join(distPath, 'index.html'), (err) => { if (err) { res.status(404).send('前端文件未找到,请先执行 npm run build'); } }); }); app.listen(PORT, '0.0.0.0', () => { console.log(`服务器运行在 http://0.0.0.0:${PORT} (NODE_ENV=${process.env.NODE_ENV || 'development'})`); });