elearning/Backend
2026-01-08 06:51:33 +00:00
..
.agent init Backend 2026-01-08 06:51:33 +00:00
prisma init Backend 2026-01-08 06:51:33 +00:00
src init Backend 2026-01-08 06:51:33 +00:00
.env.example init Backend 2026-01-08 06:51:33 +00:00
.gitignore init Backend 2026-01-08 06:51:33 +00:00
agent_skills_backend.md a 2026-01-08 03:58:29 +00:00
compose.yaml init Backend 2026-01-08 06:51:33 +00:00
package-lock.json init Backend 2026-01-08 06:51:33 +00:00
package.json init Backend 2026-01-08 06:51:33 +00:00
README.md init Backend 2026-01-08 06:51:33 +00:00

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

  1. Clone the repository
git clone <repository-url>
cd e-learning/Backend
  1. Install dependencies
npm install
  1. Setup environment variables
cp .env.example .env
# Edit .env with your configuration
  1. Start Docker services (PostgreSQL, Redis, MinIO)
docker compose up -d
  1. Run database migrations
npx prisma migrate dev
  1. Seed the database
npm run prisma:seed
  1. 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 string
  • JWT_SECRET: Secret key for JWT tokens
  • REDIS_URL: Redis connection string
  • S3_ENDPOINT: MinIO/S3 endpoint
  • CORS_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 user
  • POST /api/auth/login - Login user
  • GET /api/auth/me - Get current user profile
  • POST /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

  1. Set NODE_ENV=production
  2. Set strong JWT_SECRET
  3. Configure production database
  4. Run migrations: npx prisma migrate deploy
  5. Start with PM2: pm2 start src/server.js

📚 Resources

📄 License

MIT