A RESTful API built with Node.js and Express for managing courses. This API provides CRUD operations for courses with user authentication, role-based authorization, and image upload capabilities.
- Features
- Tech Stack
- Project Structure
- Installation
- Environment Variables
- API Endpoints
- Authentication & Authorization
- Error Handling
- Contributing
- License
- Course Management: Complete CRUD operations for courses
- User Authentication: Secure registration and login with JWT
- Role-Based Authorization: ADMIN, MANAGER, and USER roles
- Image Upload: Profile image upload support with Multer
- Input Validation: Request validation using Express Validator
- Error Handling: Centralized error handling middleware
- Password Security: Bcrypt password hashing
- CORS Enabled: Cross-origin resource sharing support
- Pagination: Support for paginated course listings
- Runtime: Node.js
- Framework: Express.js v5.1.0
- Database: MongoDB with Mongoose ODM
- Authentication: JSON Web Tokens (JWT)
- Password Hashing: Bcrypt
- Validation: Express Validator
- File Upload: Multer
- Development: Nodemon
- Others: CORS, Dotenv
courses/
โโโ controllers/
โ โโโ courses.controller.js
โ โโโ users.controller.js
โโโ middlewares/
โ โโโ allowedTo.js
โ โโโ asyncWrapper.js
โ โโโ verifyToken.js
โโโ models/
โ โโโ course.model.js
โ โโโ user.model.js
โโโ routes/
โ โโโ courses.route.js
โ โโโ users.route.js
โโโ uploads/
โ โโโ (uploaded images)
โโโ utils/
โ โโโ appError.js
โ โโโ httpStatusText.js
โ โโโ userRoles.js
โโโ .env
โโโ .gitignore
โโโ app.js
โโโ package.json
โโโ README.md
- Node.js (v18 or higher)
- MongoDB (local or MongoDB Atlas)
- npm or yarn
-
Clone the repository
git clone https://github.com/yourusername/courses-api.git cd courses-api -
Install dependencies
npm install
-
Create environment file
cp .env.example .env
-
Configure environment variables (see below)
-
Start the server
# Development mode with auto-reload npm start # Production mode node app.js
The server will start on http://localhost:4000 (or your configured PORT).
Create a .env file in the root directory:
# Server Configuration
PORT=4000
# Database
DB_URI=mongodb://localhost:27017/courses
# Or for MongoDB Atlas:
# DB_URI=mongodb+srv://username:password@cluster.mongodb.net/courses
# JWT Secret
JWT_SECRET_KEY=your_super_secret_jwt_key_here_change_in_production| Method | Endpoint | Description | Auth Required | Roles |
|---|---|---|---|---|
| GET | /api/courses |
Get all courses (with pagination) | No | - |
| GET | /api/courses/:courseId |
Get single course by ID | No | - |
| POST | /api/courses |
Create a new course | No | - |
| PATCH | /api/courses/:courseId |
Update a course | No | - |
| DELETE | /api/courses/:courseId |
Delete a course | Yes | ADMIN, MANAGER |
| Method | Endpoint | Description | Auth Required | Roles |
|---|---|---|---|---|
| GET | /api/users |
Get all users | Yes | - |
| POST | /api/users/register |
Register new user | No | - |
| POST | /api/users/login |
Login user | No | - |
Get All Courses supports pagination:
page- Page number (default: 1)limit- Items per page (default: 10)
Example: /api/courses?page=2&limit=5
Request:
POST /api/courses
Content-Type: application/json
{
"name": "Advanced Node.js Development",
"price": 99.99
}Response:
{
"status": "success",
"data": {
"course": {
"_id": "507f1f77bcf86cd799439011",
"name": "Advanced Node.js Development",
"price": 99.99
}
}
}Request:
POST /api/users/register
Content-Type: multipart/form-data
{
"firstName": "John",
"lastName": "Doe",
"email": "john.doe@example.com",
"password": "SecurePassword123",
"role": "USER",
"image": [file]
}Response:
{
"status": "success",
"data": {
"_id": "507f1f77bcf86cd799439012",
"firstName": "John",
"lastName": "Doe",
"email": "john.doe@example.com",
"role": "USER",
"image": "uploads/image-1234567890.jpg"
}
}Request:
POST /api/users/login
Content-Type: application/json
{
"email": "john.doe@example.com",
"password": "SecurePassword123"
}Response:
{
"status": "success",
"data": {
"token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9..."
}
}Request:
GET /api/courses?page=1&limit=10Response:
{
"status": "success",
"data": {
"courses": [
{
"_id": "507f1f77bcf86cd799439011",
"name": "Advanced Node.js Development",
"price": 99.99
},
{
"_id": "507f1f77bcf86cd799439013",
"name": "React Fundamentals",
"price": 79.99
}
]
}
}Request:
PATCH /api/courses/507f1f77bcf86cd799439011
Content-Type: application/json
{
"name": "Advanced Node.js & Express Development",
"price": 109.99
}Response:
{
"status": "success",
"data": {
"course": {
"_id": "507f1f77bcf86cd799439011",
"name": "Advanced Node.js & Express Development",
"price": 109.99
}
}
}Request:
DELETE /api/courses/507f1f77bcf86cd799439011
Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...Response:
{
"status": "success",
"data": {
"course": {
"_id": "507f1f77bcf86cd799439011",
"name": "Advanced Node.js & Express Development",
"price": 109.99
}
}
}After logging in, include the JWT token in the Authorization header:
Authorization: Bearer YOUR_JWT_TOKEN_HERE- USER: Default role for registered users
- MANAGER: Can delete courses
- ADMIN: Full access to all operations
The API uses consistent error responses:
{
"status": "fail",
"message": "Course not found",
"code": 404,
"data": null
}400- Bad Request (validation errors)401- Unauthorized (missing or invalid token)404- Not Found500- Internal Server Error
- Add course categories and tags
- Implement course search and filtering
- Add course reviews and ratings
- Implement user enrollment system
- Add email verification for registration
- Implement password reset functionality
- Add API rate limiting
- Create comprehensive API documentation with Swagger
- Add unit and integration tests
- Implement caching with Redis
- Add course content management (videos, documents)
Contributions are welcome! Please follow these steps:
- Fork the repository
- Create a new branch (
git checkout -b feature/amazing-feature) - Commit your changes (
git commit -m 'Add some amazing feature') - Push to the branch (
git push origin feature/amazing-feature) - Open a Pull Request
Please make sure to update tests as appropriate and adhere to the existing coding style.
This project is licensed under the ISC License - see the LICENSE file for details.
Made with โค๏ธ using Node.js and Express
For questions or support, please open an issue in the GitHub repository.