elearning/Backend/README.md
2026-01-08 06:51:33 +00:00

191 lines
4.4 KiB
Markdown

# 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**
```bash
git clone <repository-url>
cd e-learning/Backend
```
2. **Install dependencies**
```bash
npm install
```
3. **Setup environment variables**
```bash
cp .env.example .env
# Edit .env with your configuration
```
4. **Start Docker services** (PostgreSQL, Redis, MinIO)
```bash
docker compose up -d
```
5. **Run database migrations**
```bash
npx prisma migrate dev
```
6. **Seed the database**
```bash
npm run prisma:seed
```
7. **Start development server**
```bash
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
```bash
# 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
```bash
# 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
```bash
# 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
- [Prisma Documentation](https://www.prisma.io/docs)
- [Express Documentation](https://expressjs.com/)
- [JWT Documentation](https://jwt.io/)
## 📄 License
MIT