courseworx/backend/server.js
Mahmoud M. Abdalla b4d90c650f Release v1.1.0 - Course Content & Enrollment Management
New Features:
- Comprehensive course content management system
- Multi-type content support (Documents, Images, Videos, Articles, Quizzes, Certificates)
- File upload system with 100MB limit and type validation
- Quiz system with multiple question types
- Complete enrollment and subscriber management
- Status tracking (pending, active, completed, cancelled)
- Payment management (pending, paid, failed, refunded)
- Progress tracking (0-100%) with automatic updates
- Course capacity limits and validation
- Certificate issuance tracking
- Enrollment analytics and statistics

Technical Improvements:
- Enhanced file upload system with type validation
- New database models: CourseContent, QuizQuestion
- Comprehensive API endpoints for content and enrollment management
- Role-based access control for all new features
- Enhanced error handling and validation
- File management with automatic cleanup

This version provides a complete foundation for creating rich educational content and managing student enrollments.
2025-07-27 23:59:01 +03:00

83 lines
No EOL
2.5 KiB
JavaScript

const express = require('express');
const cors = require('cors');
const helmet = require('helmet');
const path = require('path');
require('dotenv').config();
const { sequelize } = require('./config/database');
// Import models to ensure they are registered with Sequelize
require('./models');
const authRoutes = require('./routes/auth');
const userRoutes = require('./routes/users');
const courseRoutes = require('./routes/courses');
const courseContentRoutes = require('./routes/courseContent');
const enrollmentRoutes = require('./routes/enrollments');
const attendanceRoutes = require('./routes/attendance');
const assignmentRoutes = require('./routes/assignments');
const app = express();
const PORT = process.env.PORT || 5000;
// Middleware
app.use(helmet());
app.use(cors({
origin: process.env.CORS_ORIGIN || 'http://localhost:3000',
credentials: true
}));
app.use(express.json({ limit: '10mb' }));
app.use(express.urlencoded({ extended: true, limit: '10mb' }));
// Static files
app.use('/uploads', express.static(path.join(__dirname, 'uploads')));
// Routes
app.use('/api/auth', authRoutes);
app.use('/api/users', userRoutes);
app.use('/api/courses', courseRoutes);
app.use('/api/course-content', courseContentRoutes);
app.use('/api/enrollments', enrollmentRoutes);
app.use('/api/attendance', attendanceRoutes);
app.use('/api/assignments', assignmentRoutes);
// Health check endpoint
app.get('/api/health', (req, res) => {
res.json({ status: 'OK', message: 'CourseWorx API is running' });
});
// Error handling middleware
app.use((err, req, res, next) => {
console.error(err.stack);
res.status(500).json({
error: 'Something went wrong!',
message: process.env.NODE_ENV === 'development' ? err.message : 'Internal server error'
});
});
// 404 handler
app.use('*', (req, res) => {
res.status(404).json({ error: 'Route not found' });
});
// Database connection and server start
const startServer = async () => {
try {
await sequelize.authenticate();
console.log('✅ Database connection established successfully.');
// Sync database (in development)
if (process.env.NODE_ENV === 'development') {
await sequelize.sync({ alter: true });
console.log('✅ Database synchronized.');
}
app.listen(PORT, () => {
console.log(`🚀 Server running on port ${PORT}`);
console.log(`📱 API available at http://localhost:${PORT}/api`);
});
} catch (error) {
console.error('❌ Unable to connect to the database:', error);
process.exit(1);
}
};
startServer();