first commit
This commit is contained in:
128
backend/server.js
Normal file
128
backend/server.js
Normal file
@@ -0,0 +1,128 @@
|
||||
const express = require('express');
|
||||
const cors = require('cors');
|
||||
const session = require('express-session');
|
||||
const MySQLStore = require('express-mysql-session')(session);
|
||||
const path = require('path');
|
||||
require('dotenv').config();
|
||||
|
||||
// 导入路由
|
||||
const authRoutes = require('./routes/auth');
|
||||
const studentRoutes = require('./routes/student');
|
||||
const teacherRoutes = require('./routes/teacher');
|
||||
const adminRoutes = require('./routes/admin');
|
||||
|
||||
// 数据库配置
|
||||
const db = require('./config/database');
|
||||
|
||||
const app = express();
|
||||
const PORT = process.env.PORT || 3000;
|
||||
|
||||
// 中间件
|
||||
app.use(cors({
|
||||
origin: 'http://localhost:3000',
|
||||
credentials: true
|
||||
}));
|
||||
app.use(express.json());
|
||||
app.use(express.urlencoded({ extended: true }));
|
||||
|
||||
// 会话配置
|
||||
const sessionStore = new MySQLStore({
|
||||
expiration: 86400000, // 1天
|
||||
createDatabaseTable: true,
|
||||
schema: {
|
||||
tableName: 'sessions',
|
||||
columnNames: {
|
||||
session_id: 'session_id',
|
||||
expires: 'expires',
|
||||
data: 'data'
|
||||
}
|
||||
}
|
||||
}, db.pool);
|
||||
|
||||
app.use(session({
|
||||
key: 'session_cookie',
|
||||
secret: process.env.SESSION_SECRET || 'your-secret-key',
|
||||
store: sessionStore,
|
||||
resave: false,
|
||||
saveUninitialized: false,
|
||||
cookie: {
|
||||
maxAge: 86400000,
|
||||
httpOnly: true,
|
||||
secure: process.env.NODE_ENV === 'production'
|
||||
}
|
||||
}));
|
||||
|
||||
// 静态文件服务
|
||||
app.use(express.static(path.join(__dirname, '../frontend')));
|
||||
|
||||
// 重定向旧路径 /frontend/html/* 到 /html/*
|
||||
app.get('/frontend/html/*', (req, res) => {
|
||||
const path = req.params[0];
|
||||
res.redirect(`/html/${path}`);
|
||||
});
|
||||
|
||||
// 路由
|
||||
app.use('/api/auth', authRoutes);
|
||||
app.use('/api/student', studentRoutes);
|
||||
app.use('/api/teacher', teacherRoutes);
|
||||
app.use('/api/admin', adminRoutes);
|
||||
|
||||
// 认证中间件
|
||||
const { requireAuth, requireRole } = require('./middleware/auth');
|
||||
|
||||
// 页面路由
|
||||
app.get('/', (req, res) => {
|
||||
res.sendFile(path.join(__dirname, '../frontend/html/login.html'));
|
||||
});
|
||||
|
||||
app.get('/dashboard', requireAuth, (req, res) => {
|
||||
// 根据用户角色重定向到不同的仪表板
|
||||
const role = req.session.user?.role;
|
||||
|
||||
switch (role) {
|
||||
case 'student':
|
||||
res.redirect('/html/student_dashboard.html');
|
||||
break;
|
||||
case 'teacher':
|
||||
res.redirect('/html/teacher_dashboard.html');
|
||||
break;
|
||||
case 'admin':
|
||||
res.redirect('/html/admin_dashboard.html');
|
||||
break;
|
||||
default:
|
||||
// 如果没有角色信息,重定向到登录页面
|
||||
res.redirect('/');
|
||||
break;
|
||||
}
|
||||
});
|
||||
|
||||
// 学生页面路由
|
||||
app.get('/student/*', requireAuth, requireRole(['student', 'admin', 'teacher']), (req, res, next) => {
|
||||
next();
|
||||
});
|
||||
|
||||
// 教师页面路由
|
||||
app.get('/teacher/*', requireAuth, requireRole(['teacher', 'admin']), (req, res, next) => {
|
||||
next();
|
||||
});
|
||||
|
||||
// 管理员页面路由
|
||||
app.get('/admin/*', requireAuth, requireRole(['admin']), (req, res, next) => {
|
||||
next();
|
||||
});
|
||||
|
||||
// 404处理
|
||||
app.use((req, res) => {
|
||||
res.status(404).json({ error: 'Not found' });
|
||||
});
|
||||
|
||||
// 错误处理
|
||||
app.use((err, req, res, next) => {
|
||||
console.error(err.stack);
|
||||
res.status(500).json({ error: 'Internal server error' });
|
||||
});
|
||||
|
||||
app.listen(PORT, () => {
|
||||
console.log(`Server running on port ${PORT}`);
|
||||
console.log(`访问地址: http://localhost:${PORT}`);
|
||||
});
|
||||
Reference in New Issue
Block a user