| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859 |
- const express = require('express');
- const cors = require('cors');
- const bodyParser = require('body-parser');
- const path = require('path');
- const { initDatabase } = require('./database/init');
- const issueRoutes = require('./routes/issues');
- const projectRoutes = require('./routes/projects');
- const app = express();
- const PORT = process.env.PORT || 3000;
- // 中介軟體設定
- app.use(cors());
- app.use(bodyParser.json());
- app.use(bodyParser.urlencoded({ extended: true }));
- // 靜態檔案服務
- app.use(express.static(path.join(__dirname, 'public')));
- // 路由設定
- app.use('/api/issues', issueRoutes);
- app.use('/api/projects', projectRoutes);
- // 首頁路由
- app.get('/', (req, res) => {
- res.sendFile(path.join(__dirname, 'public', 'index.html'));
- });
- // 錯誤處理中介軟體
- app.use((err, req, res, next) => {
- console.error(err.stack);
- res.status(500).json({
- success: false,
- message: '伺服器內部錯誤',
- error: process.env.NODE_ENV === 'development' ? err.message : '請稍後再試'
- });
- });
- // 404 處理
- app.use((req, res) => {
- res.status(404).json({
- success: false,
- message: '找不到請求的資源'
- });
- });
- // 初始化資料庫並啟動伺服器
- initDatabase().then(() => {
- app.listen(PORT, () => {
- console.log(`🚀 MAA測試問題追蹤系統已啟動`);
- console.log(`📍 伺服器運行在: http://localhost:${PORT}`);
- console.log(`📊 管理介面: http://localhost:${PORT}`);
- });
- }).catch(err => {
- console.error('❌ 資料庫初始化失敗:', err);
- process.exit(1);
- });
- module.exports = app;
|