A full-stack RESTful API for course and user management with JWT authentication, built with Node.js, Express.js, and MongoDB.
- β User Registration with email validation
- β Secure Login with JWT tokens
- β Password hashing with bcrypt
- β Protected routes with middleware
- β User profile management
- β Full CRUD operations for courses
- β Course listing with pagination
- β Course enrollment system
- β Public and protected endpoints
- β JWT token authentication
- β Password encryption (bcrypt)
- β Input validation with express-validator
- β CORS configuration
- β Error handling with express-async-handler
- β JSend response format
- Node.js (v14 or higher)
- MongoDB Atlas account or local MongoDB
- Git
-
Clone the repository
git clone https://github.com/MONOCODE-V/course-management-api.git cd course-management-api -
Install dependencies
npm install
-
Environment Setup
# Copy environment template cp .env.example .env # Edit .env file with your configuration # MONGODB_URI=your_mongodb_connection_string # JWT_SECRET=your_super_secret_key # PORT=3000
-
Start the server
# Development with auto-restart npm run dev # Production npm start
http://localhost:3000/api
POST /auth/register
Content-Type: application/json
{
"name": "John Doe",
"email": "john@example.com",
"password": "MyPassword123!",
"age": 25
}POST /auth/login
Content-Type: application/json
{
"email": "john@example.com",
"password": "MyPassword123!"
}GET /coursesRequires Authorization header: Bearer <jwt_token>
GET /profile/me
Authorization: Bearer <jwt_token>PATCH /profile/me
Authorization: Bearer <jwt_token>
Content-Type: application/json
{
"name": "Updated Name",
"email": "new@example.com"
}# Create Course
POST /courses
Authorization: Bearer <jwt_token>
# Update Course
PATCH /courses/:courseId
Authorization: Bearer <jwt_token>
# Delete Course
DELETE /courses/:courseId
Authorization: Bearer <jwt_token>course-management-api/
βββ π controllers/ # Route handlers
β βββ auth.controllers.js # Authentication logic
β βββ course.controllers.js # Course CRUD operations
β βββ user.profile.controllers.js # Profile management
βββ π data/ # Database schemas
β βββ coursesSchema.js # Course model
β βββ userSchema.js # User model
βββ π middleware/ # Custom middleware
β βββ auth.js # JWT authentication
βββ π routes/ # API routes
β βββ authRoutes.js # Auth endpoints
β βββ courses.route.js # Course endpoints
β βββ profileRoutes.js # Profile endpoints
βββ π index.js # Server entry point
βββ π package.json # Dependencies
βββ π .env.example # Environment template
βββ π README.md # This file
{
name: String, // Required, 2-50 characters
email: String, // Required, unique, validated
password: String, // Required, hashed with bcrypt
age: Number, // Required, 13-120
role: String, // Enum: ['user', 'admin', 'moderator']
enrolledCourses: [ObjectId], // References to Course
createdAt: Date // Auto-generated
}{
title: String, // Required
price: Number, // Required, positive
description: String, // Optional
instructor: String, // Required
duration: Number, // In hours
level: String, // Enum: ['beginner', 'intermediate', 'advanced']
createdAt: Date // Auto-generated
}POST http://localhost:3000/api/auth/register
Content-Type: application/json
{
"name": "Test User",
"email": "test@example.com",
"password": "TestPassword123!",
"age": 25
}POST http://localhost:3000/api/auth/login
Content-Type: application/json
{
"email": "test@example.com",
"password": "TestPassword123!"
}GET http://localhost:3000/api/profile/me
Authorization: Bearer <your_jwt_token_from_login># Start development server with auto-reload
npm run dev
# Start production server
npm start
# Install dependencies
npm installCreate a .env file in the root directory:
# Database
MONGODB_URI=mongodb+srv://username:password@cluster.mongodb.net/dbname
# JWT Configuration
JWT_SECRET=your-super-secret-jwt-key-make-it-very-long-and-random
JWT_EXPIRES_IN=7d
# Server Configuration
PORT=3000
NODE_ENV=development
# Password Hashing
BCRYPT_ROUNDS=12- express - Web framework for Node.js
- mongoose - MongoDB object modeling
- jsonwebtoken - JWT token generation and verification
- bcryptjs - Password hashing
- express-validator - Input validation middleware
- express-async-handler - Async error handling
- cors - Cross-Origin Resource Sharing
- dotenv - Environment variable management
- nodemon - Development server with auto-reload
- Password Hashing: Uses bcrypt with configurable rounds
- JWT Authentication: Stateless token-based auth
- Input Validation: Server-side validation for all inputs
- CORS Protection: Configurable cross-origin requests
- Error Handling: Centralized error management
- Environment Variables: Sensitive data protection
- Fork the repository
- Create your feature branch (
git checkout -b feature/AmazingFeature) - Commit your changes (
git commit -m 'Add some AmazingFeature') - Push to the branch (
git push origin feature/AmazingFeature) - Open a Pull Request
This project is licensed under the MIT License - see the LICENSE file for details.
MONOCODE-V
- GitHub: @MONOCODE-V
- Email: minamagdypro@gmail.com
- Express.js team for the amazing framework
- MongoDB team for the database solution
- JWT.io for token authentication standards
- The Node.js community for continuous support
β Star this repository if it helped you!