diff --git a/server.js b/server.js new file mode 100644 index 0000000..5554dc3 --- /dev/null +++ b/server.js @@ -0,0 +1,26 @@ +const express = require('express'); +const path = require('path'); +const bodyParser = require('body-parser'); +const router = require('./router'); +const app = express(); + +// 中间件 +app.use(bodyParser.json()); +app.use(bodyParser.urlencoded({ extended: true })); + +// API 路由 +app.use('/api', router); + +// 静态文件 +app.use(express.static(path.join(__dirname, 'public'))); + +// 处理 SPA 路由 +app.get('*', (req, res) => { + res.sendFile(path.join(__dirname, 'public', 'index.html')); +}); + +// 启动服务器 +const PORT = process.env.PORT || 3000; +app.listen(PORT, () => { + console.log(`Server running on port ${PORT}`); +});