{"id":29173234,"url":"https://github.com/shaharearsabbir/learnosphere-server","last_synced_at":"2026-04-13T21:05:10.951Z","repository":{"id":301087071,"uuid":"1007899260","full_name":"ShaharearSabbir/LearnOsphere-server","owner":"ShaharearSabbir","description":"Driving the Learnosphere experience. This repository contains the server-side logic and database interactions for our educational platform, ensuring secure data management and scalable performance.","archived":false,"fork":false,"pushed_at":"2025-06-25T04:02:35.000Z","size":24,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":0,"default_branch":"main","last_synced_at":"2025-06-25T05:18:54.858Z","etag":null,"topics":["express","jwt","mongodb"],"latest_commit_sha":null,"homepage":"https://learnosphere-server.vercel.app/","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/ShaharearSabbir.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2025-06-24T17:51:02.000Z","updated_at":"2025-06-25T04:02:38.000Z","dependencies_parsed_at":"2025-06-25T05:18:56.286Z","dependency_job_id":"f23074ca-6c06-4a9c-8798-86cbbce3dc44","html_url":"https://github.com/ShaharearSabbir/LearnOsphere-server","commit_stats":null,"previous_names":["shaharearsabbir/learnosphere-server"],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/ShaharearSabbir/LearnOsphere-server","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ShaharearSabbir%2FLearnOsphere-server","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ShaharearSabbir%2FLearnOsphere-server/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ShaharearSabbir%2FLearnOsphere-server/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ShaharearSabbir%2FLearnOsphere-server/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/ShaharearSabbir","download_url":"https://codeload.github.com/ShaharearSabbir/LearnOsphere-server/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ShaharearSabbir%2FLearnOsphere-server/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":262978722,"owners_count":23394016,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["express","jwt","mongodb"],"created_at":"2025-07-01T14:11:15.508Z","updated_at":"2026-04-13T21:05:10.670Z","avatar_url":"https://github.com/ShaharearSabbir.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# LearnOsphere - Backend\n\nThis repository contains the backend for the LearnOsphere online learning platform. It provides the API endpoints and handles data management for the frontend application.\n\n---\n\n## ✨ Top Features\n\n* **Robust User Authentication \u0026 Authorization:** Implements secure user registration, login, and access control leveraging **JSON Web Tokens (JWT)**, specifically setting JWTs as HTTP-only cookies. Includes a dedicated `varifyToken` middleware for route protection.\n* **Atomic Enrollment \u0026 Unenrollment with MongoDB Transactions:** Manages user enrollment and unenrollment in courses using **MongoDB transactions**. This ensures critical data consistency across `enrollments`, `courses` (updating `totalEnrollment` and `RemainingSeat`), and `user` collections (modifying `totalEnrolled` and `enrolledCourseIds`) in a single, all-or-nothing operation.\n* **Advanced Course Management \u0026 Data Aggregation:** Offers sophisticated course retrieval with comprehensive filtering (by `free`/`price`, `category`), sorting (`createdAt`), and pagination options using a powerful **aggregation pipeline**. Detailed course information includes aggregated `numberOfReview` and `averageRating` through `$lookup` operations with the `reviews` collection.\n* **Dynamic User \u0026 Review Data Enrichment:** Enhances user profiles by aggregating `enrolledCourseIds` from the `enrollments` collection and enriches course reviews by incorporating reviewer `displayName` and `photoURL` via `$lookup` with the `user` collection.\n* **Mentor-Specific Course Access Control:** Provides secure retrieval of courses posted by a specific mentor, with access protected by the `varifyToken` middleware.\n\n---\n\n## 🛠️ Technologies Used\n\nThe LearnOsphere backend is built with a focus on efficiency, scalability, and security.\n\n* **Node.js:** The JavaScript runtime environment.\n* **Express.js:** A fast, unopinionated, minimalist web framework for Node.js, used for building robust APIs.\n* **MongoDB:** A NoSQL database used for storing all application data.\n    * **`MongoClient` \u0026 `ServerApiVersion`:** Directly interacts with MongoDB for database operations.\n    * **`ObjectId`:** Used for handling MongoDB's unique document identifiers.\n    * **Transactions:** Employed for critical operations like enrollment/unenrollment to maintain data integrity across multiple collections.\n* **JSON Web Token (`jsonwebtoken`):** For secure authentication and authorization, handling token signing and verification.\n* **CORS (`cors`):** Middleware configured to allow Cross-Origin Resource Sharing from specified frontend origins (`http://localhost:5173`, `https://learn-osphere.web.app`).\n* **Dotenv (`dotenv`):** For loading environment variables (like `PORT`, `MONGODB_URI`, `JWT_ACCESS_SECRET`) from a `.env` file, keeping sensitive information out of the codebase.\n* **Cookie-parser (`cookie-parser`):** Middleware for parsing cookies attached to the client request object, essential for handling JWTs stored as HTTP-only cookies.\n\n---\n\n## 🚀 Getting Started\n\nTo get the backend server up and running on your local machine, follow these simple steps.\n\n### Prerequisites\n\nMake sure you have **Node.js** and **npm** installed. **MongoDB** should also be installed and running.\n\n* **Node.js \u0026 npm:**\n    ```bash\n    npm install npm@latest -g\n    ```\n* **MongoDB:** [Install MongoDB](https://docs.mongodb.com/manual/installation/)\n\n### Running the Server\n\n1.  Create a **`.env`** file in the root of your project and add your environment variables:\n    ```env\n    PORT=3000\n    DB_USER=your_mongodb_username\n    DB_PASS=your_mongodb_password\n    JWT_ACCESS_SECRET=your_jwt_secret_key\n    NODE_ENV=development # or production\n    ```\n    * `PORT`: The port your backend server will run on (defaulting to 3000 if not specified).\n    * `DB_USER` \u0026 `DB_PASS`: Your MongoDB Atlas cluster username and password.\n    * `JWT_ACCESS_SECRET`: A strong, random string for signing JWTs. **Generate a complex one for production!**\n    * `NODE_ENV`: Set to `production` in production environments for secure cookie handling (`secure` and `sameSite` settings).\n2.  Start the backend server:\n    ```bash\n    npm start\n    ```\n    The server will typically run on the port specified in your `.env` file (e.g., `http://localhost:3000`).\n\n---\n\n## 🗺️ API Endpoints\n\nHere's a detailed overview of the API endpoints provided by this backend:\n\n| Method | Endpoint | Description | Authentication |\n| :------- | :----------------------------------- | :----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | :------------- |\n| `POST` | `/jwt` | Generates and sets a JWT cookie for authenticated users. | Public |\n| `POST` | `/logout` | Clears the JWT cookie, effectively logging out the user. | Public |\n| `POST` | `/user` | Creates a new user in the database. | Public |\n| `GET` | `/user/:uid` | Retrieves user details, including a list of enrolled course IDs from the `enrollments` collection. | Public |\n| `PUT` | `/user/:uid` | Updates an existing user's data. | Public |\n| `POST` | `/category` | Adds a new course category. | Public |\n| `GET` | `/categories` | Retrieves a list of course categories. Supports query parameters: `limit`, `sortBy`, `orderBy`. | Public |\n| `POST` | `/course` | Creates a new course entry in the database. | Public |\n| `GET` | `/course/:id` | Retrieves details for a specific course by its `_id`, including aggregated review count and average rating. | Public |\n| `GET` | `/course` | Retrieves all courses, with aggregated review count and average rating for each. | Public |\n| `GET` | `/courses/:uid` | Retrieves courses posted by a specific mentor (`mentorUID`). | Requires JWT |\n| `GET` | `/courses` | Retrieves courses with advanced filtering, sorting, and limiting options. Query parameters: `filterBy` (e.g., \"free\", \"price\", or category name), `limit`, `sortBy`, `orderBy`. | Public |\n| `PUT` | `/course/:id` | Updates an existing course by its `_id`. | Public |\n| `DELETE` | `/course/:id` | Deletes a course by its `_id`. | Public |\n| `POST` | `/review` | Adds a new review for a course. | Public |\n| `GET` | `/reviewsByCourseId/:id` | Retrieves all reviews for a given `courseId`, including reviewer's display name and photo. | Public |\n| `DELETE` | `/review/:id` | Deletes a specific review by its `_id`. | Public |\n| `POST` | `/enrollment` | Handles user enrollment and unenrollment in courses. Requires `uid`, `courseId`, and `enroll` status in the body. Utilizes **MongoDB Transactions** for atomic operations across multiple collections. | Public |\n| `GET` | `/enrollments/:uid` | Retrieves all enrollments for a specific user, including details of the enrolled courses. | Requires JWT |\n| `DELETE` | `/enrollment/:id` | Deletes a specific enrollment record by its `_id`. | Public |\n| `POST` | `/blog` | Creates a new blog post. | Public |\n| `GET` | `/blogs` | Retrieves all blog posts, including the author's display name and photo. | Public |\n| `GET` | `/` | Basic root endpoint to confirm the server is running. | Public |\n\n---\n\n## 🤝 Contributing\n\nContributions are what make the open-source community such an amazing place to learn, inspire, and create. Any contributions you make are **greatly appreciated**.\n\nIf you have a suggestion that would make this better, please fork the repo and create a pull request. You can also simply open an issue with the tag \"enhancement\".\n\n1.  Fork the Project\n2.  Create your Feature Branch (`git checkout -b feature/AmazingFeature`)\n3.  Commit your Changes (`git commit -m 'Add some AmazingFeature'`)\n4.  Push to the Branch (`git push origin feature/AmazingFeature`)\n5.  Open a Pull Request\n\n---\n\n## 📄 License\n\nDistributed under the MIT License. See `LICENSE` for more information.\n\n---\n\n## 📞 Contact\n\nShaharear Rahman Sabbir - imshaharear@gmail.com\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fshaharearsabbir%2Flearnosphere-server","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fshaharearsabbir%2Flearnosphere-server","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fshaharearsabbir%2Flearnosphere-server/lists"}