This repository contains the backend implementation of a course selling app built using Node.js, Express, and MongoDB. The API enables admins to manage courses, including creating, updating, deleting, and archiving courses to a deleted records collection.
-
Admin Authentication:
- Signup and login for admins with secure password hashing using bcrypt.
- Authentication using JWT tokens for secure access.
-
Course Management:
- Create new courses.
- Update existing courses.
- Delete courses and transfer them to a separate
DeletedCoursescollection.
-
Middleware:
- Admin middleware to protect routes and ensure only authenticated admins can access them.
- Node.js installed (v16 or later recommended).
- MongoDB database instance (local or cloud-based).
- Environment variables configured in a
.envfile.
-
Clone the repository:
git clone <repository-url> cd <repository-folder>
-
Install dependencies:
npm install
-
Create a
.envfile in the root directory with the following content:MONGO_URL=<your_mongo_database_url> ADMIN_SECRET=<your_admin_jwt_secret> USER_SECRET=<your_user_jwt_secret>
-
Start the server:
npm start
-
The API will be running on
http://localhost:3000(or your configured port).
POST /admin/signup
- Body Parameters:
{ "email": "admin@example.com", "password": "securepassword", "firstname": "John", "lastname": "Doe" } - Response:
- Success:
200 OK{ "message": "Admin created successfully" } - Error:
400 Bad Request{ "message": "Email already exists" }
- Success:
POST /admin/signin
- Body Parameters:
{ "email": "admin@example.com", "password": "securepassword" } - Response:
- Success:
200 OK{ "token": "<JWT>", "message": "Login successful" } - Error:
400 Bad Request{ "message": "Incorrect password" }or{ "message": "Email not found" }
- Success:
POST /admin/course
- Headers:
- Authorization: Bearer
<JWT>
- Authorization: Bearer
- Body Parameters:
{ "title": "Course Title", "description": "Course Description", "price": 100, "imageurl": "https://example.com/image.jpg" } - Response:
- Success:
200 OK{ "message": "Course created successfully" } - Error:
400 Bad Request{ "message": "Course already exists" }
- Success:
PUT /admin/course
- Headers:
- Authorization: Bearer
<JWT>
- Authorization: Bearer
- Body Parameters:
{ "courseId": "<course_id>", "title": "Updated Title", "description": "Updated Description", "price": 150, "imageurl": "https://example.com/updated-image.jpg" } - Response:
- Success:
200 OK{ "message": "Course updated successfully" } - Error:
400 Bad Request{ "message": "Course does not exist" }
- Success:
DELETE /admin/course
- Headers:
- Authorization: Bearer
<JWT>
- Authorization: Bearer
- Body Parameters:
{ "courseId": "<course_id>" } - Response:
- Success:
200 OK{ "message": "Course deleted successfully" } - Error:
400 Bad Request{ "message": "Course not found" }
- Success:
const adminSchema = new Schema({
email: { type: String, required: true, unique: true },
password: { type: String, required: true },
firstname: String,
lastname: String
});const courseSchema = new Schema({
title: { type: String, required: true, unique: true },
description: String,
price: Number,
imageurl: String,
creatorId: { type: Schema.Types.ObjectId, ref: 'Admin' }
});const deletedCourseSchema = new Schema({
title: String,
description: String,
price: Number,
imageurl: String,
deleterId: { type: Schema.Types.ObjectId, ref: 'Admin' }
});This project is licensed under the MIT License.