35 lines
1.0 KiB
JavaScript
35 lines
1.0 KiB
JavaScript
const express = require('express');
|
|
const cors = require('cors');
|
|
const path = require('path');
|
|
|
|
const config = require('./server/config');
|
|
require('./server/database'); // 初始化数据库
|
|
const { authenticateToken } = require('./server/middleware/auth');
|
|
const authRouter = require('./server/routes/auth');
|
|
const calendarRouter = require('./server/routes/calendar');
|
|
const { registerShutdown } = require('./server/utils/shutdown');
|
|
|
|
const app = express();
|
|
|
|
app.use(cors());
|
|
app.use(express.json());
|
|
app.use(express.static(path.join(__dirname, 'public')));
|
|
|
|
app.use('/api/auth', authRouter);
|
|
app.use('/api/calendar', authenticateToken, calendarRouter);
|
|
|
|
// 全局错误处理
|
|
app.use((err, req, res, next) => {
|
|
console.error('未处理的错误:', err);
|
|
res.status(500).json({ success: false, error: '服务器内部错误' });
|
|
});
|
|
|
|
const server = app.listen(config.PORT, () => {
|
|
console.log(`服务器运行在 http://localhost:${config.PORT}`);
|
|
console.log(`数据库文件: ${config.DB_PATH}`);
|
|
});
|
|
|
|
registerShutdown(server);
|
|
|
|
module.exports = app;
|