4.4 KiB
4.4 KiB
E-Learning Backend
Backend API for the E-Learning Platform built with Node.js, Express, Prisma, and PostgreSQL.
🚀 Features
- Authentication & Authorization: JWT-based authentication with role-based access control
- Course Management: Create, manage, and publish courses with chapters and lessons
- Multi-Language Support: Thai and English content support
- Quiz System: Interactive quizzes with multiple attempts and score policies
- Progress Tracking: Track student progress and issue certificates
- File Upload: Support for video lessons and attachments (MinIO/S3)
- Caching: Redis integration for improved performance
- Security: Helmet, CORS, rate limiting, and input validation
📋 Prerequisites
- Node.js >= 18.0.0
- PostgreSQL >= 14
- Redis (optional, for caching)
- MinIO or S3 (for file storage)
🛠️ Installation
- Clone the repository
git clone <repository-url>
cd e-learning/Backend
- Install dependencies
npm install
- Setup environment variables
cp .env.example .env
# Edit .env with your configuration
- Start Docker services (PostgreSQL, Redis, MinIO)
docker compose up -d
- Run database migrations
npx prisma migrate dev
- Seed the database
npm run prisma:seed
- Start development server
npm run dev
The server will start on http://localhost:4000
📁 Project Structure
Backend/
├── prisma/
│ ├── migrations/ # Database migrations
│ ├── schema.prisma # Database schema
│ └── seed.js # Database seeding
├── src/
│ ├── config/ # Configuration files
│ │ ├── database.js # Prisma client
│ │ ├── logger.js # Winston logger
│ │ └── redis.js # Redis client
│ ├── controllers/ # Request handlers
│ ├── middleware/ # Express middleware
│ │ ├── auth.js # Authentication & authorization
│ │ └── errorHandler.js
│ ├── routes/ # Route definitions
│ ├── services/ # Business logic
│ ├── utils/ # Utility functions
│ │ └── jwt.js # JWT utilities
│ ├── validators/ # Input validation
│ ├── app.js # Express app setup
│ └── server.js # Server entry point
├── tests/ # Test files
├── logs/ # Log files
├── .env.example # Environment variables template
├── package.json
└── README.md
🔑 Environment Variables
See .env.example for all available environment variables.
Key variables:
DATABASE_URL: PostgreSQL connection stringJWT_SECRET: Secret key for JWT tokensREDIS_URL: Redis connection stringS3_ENDPOINT: MinIO/S3 endpointCORS_ORIGIN: Allowed CORS origins
🧪 Testing
# Run all tests
npm test
# Run tests in watch mode
npm run test:watch
# Run tests with coverage
npm test -- --coverage
📝 API Documentation
Authentication
POST /api/auth/register- Register new userPOST /api/auth/login- Login userGET /api/auth/me- Get current user profilePOST /api/auth/logout- Logout user
Health Check
GET /health- Server health status
🔐 Default Credentials
After seeding the database, you can use these credentials:
- Admin:
admin/admin123 - Instructor:
instructor/admin123 - Student:
student/admin123
🛠️ Development
# Start dev server with auto-reload
npm run dev
# Run linter
npm run lint
# Format code
npm run format
# Open Prisma Studio (database GUI)
npm run prisma:studio
📦 Database Commands
# Generate Prisma Client
npm run prisma:generate
# Create migration
npx prisma migrate dev --name migration_name
# Apply migrations
npx prisma migrate deploy
# Reset database (development only)
npx prisma migrate reset
# Seed database
npm run prisma:seed
🚀 Production Deployment
- Set
NODE_ENV=production - Set strong
JWT_SECRET - Configure production database
- Run migrations:
npx prisma migrate deploy - Start with PM2:
pm2 start src/server.js
📚 Resources
📄 License
MIT