{"id":25860631,"url":"https://github.com/shakilofficial/blog-platform-backend","last_synced_at":"2026-04-09T18:54:34.464Z","repository":{"id":268755970,"uuid":"905362917","full_name":"Shakilofficial/blog-platform-backend","owner":"Shakilofficial","description":"A backend for a blogging platform with user roles, authentication, and CRUD operations for blogs. Built with Node.js, Express, MongoDB, and TypeScript.","archived":false,"fork":false,"pushed_at":"2024-12-21T05:48:57.000Z","size":114,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-10-09T22:14:16.523Z","etag":null,"topics":["backend","blog","expressjs","jesonwebtoken","jwt-authentication","mongodb","mongoose","nodejs","restful-api","typescript","vercel-deployment","zod"],"latest_commit_sha":null,"homepage":"https://blog-platform-backend-shakilofficial-mrshakil.vercel.app","language":"TypeScript","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/Shakilofficial.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":"2024-12-18T17:09:46.000Z","updated_at":"2024-12-21T05:49:01.000Z","dependencies_parsed_at":"2025-08-21T16:43:17.576Z","dependency_job_id":"6de9724d-3db9-4c94-9701-34e4ae045529","html_url":"https://github.com/Shakilofficial/blog-platform-backend","commit_stats":null,"previous_names":["shakilofficial/blog-platform-backend"],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/Shakilofficial/blog-platform-backend","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Shakilofficial%2Fblog-platform-backend","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Shakilofficial%2Fblog-platform-backend/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Shakilofficial%2Fblog-platform-backend/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Shakilofficial%2Fblog-platform-backend/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Shakilofficial","download_url":"https://codeload.github.com/Shakilofficial/blog-platform-backend/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Shakilofficial%2Fblog-platform-backend/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":279002130,"owners_count":26083307,"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","status":"online","status_checked_at":"2025-10-09T02:00:07.460Z","response_time":59,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"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":["backend","blog","expressjs","jesonwebtoken","jwt-authentication","mongodb","mongoose","nodejs","restful-api","typescript","vercel-deployment","zod"],"created_at":"2025-03-01T22:36:03.468Z","updated_at":"2025-10-09T22:14:17.010Z","avatar_url":"https://github.com/Shakilofficial.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"![Blog Project](https://res.cloudinary.com/dcyupktj6/image/upload/v1734650573/dd0z865lkiguyuwtbzsc.jpg)\n\n**Blog Platform Backend** is a robust backend service designed for a blogging platform that supports multiple user roles, user authentication, and the management of blog posts. Built with **Node.js**, **Express**, **MongoDB**, and **TypeScript**, this project provides a powerful API for managing users, blogs, and administrative functions with full CRUD capabilities. The backend is structured for scalability, efficiency, and security.\n\nKey features include user registration, login, role-based access control (admin and user roles), and blog post management. It also implements **Zod** for data validation and robust error handling, ensuring seamless operation and a clean user experience.\n\n## Features\n\n- **User Registration \u0026 Login**: JWT-based authentication for secure user login and token management.\n- **Role-Based Access Control**: Differentiates between regular users and admin roles, with admins having more extensive permissions.\n- **CRUD Operations for Blogs**: Allows users to create, update, delete, and read blog posts.\n- **Global Error Handling**: Custom error classes to handle various types of errors such as validation, duplicate data, and MongoDB-specific errors.\n- **Zod Validation**: Type-safe validation library for ensuring data integrity in API requests.\n- **Query Builder**: Built-in support for filtering, searching, and paginating blog posts.\n\n## Index\n\n1. [Project Title](#project-title)\n2. [Description](#description)\n3. [Features](#features)\n4. [Installation](#installation)\n5. [Usage](#usage)\n6. [API Endpoints](#api-endpoints)\n   - [Authentication](#authentication)\n     - [Register User](#register-user)\n     - [Login User](#login-user)\n   - [User Management](#user-management)\n     - [Get All Users](#get-all-users)\n     - [Get User Profile](#get-user-profile)\n     - [Update User Profile](#update-user-profile)\n   - [Blog Management](#blog-management)\n     - [Get All Blogs](#get-all-blogs)\n     - [Create Blog](#create-blog)\n     - [Update Blog](#update-blog)\n     - [Delete Blog](#delete-blog)\n   - [Admin Routes](#admin-routes)\n     - [Block User](#block-user)\n     - [Delete Blog](#delete-blog-admin)\n7. [Error Handling](#error-handling)\n8. [Authentication](#authentication)\n9. [Contribution Guidelines](#contribution-guidelines)\n10. [Contact](#contact)\n11. [License](#license)\n\n## Installation\n\n### Prerequisites\n\nBefore you begin, ensure you have the following installed:\n\n- **Node.js**: Version 18 or higher\n- **npm**: Node package manager\n- **MongoDB**: Either a local instance or MongoDB Atlas for a cloud-based solution\n\n### Steps\n\n1. Clone the repository to your local machine:\n\n   ```bash\n   git clone https://github.com/Shakilofficial/blog-platform-backend.git\n   cd blog-platform-backend\n   ```\n\n2. Install the necessary dependencies:\n\n   ```bash\n   npm install\n   ```\n\n3. Set up your environment variables in a `.env` file. Example:\n\n   ```env\n   PORT=5000\n   MONGO_URI=mongodb://localhost:27017/blog-platform\n   JWT_SECRET=your_jwt_secret_key\n   ```\n\n4. Compile TypeScript:\n\n   ```bash\n   npm run build\n   ```\n\n5. Start the server:\n   - For development:\n     ```bash\n     npm run dev\n     ```\n   - For production:\n     ```bash\n     npm run start:prod\n     ```\n\n## Usage\n\nOnce the server is running, you can access the API at `http://localhost:\u003cport\u003e/api`, where `\u003cport\u003e` is defined in your `.env` file (default is `5000`).\n\n- **Development Mode**: The server will automatically reload with changes.\n- **Production Mode**: The server will be optimized for performance.\n\n## API Endpoints\n\nHere’s a comprehensive list of the available API routes:\n\n### **Auth Routes**\n\n| **Method** | **Endpoint**         | **Description**               |\n| ---------- | -------------------- | ----------------------------- |\n| `POST`     | `/api/auth/register` | Register a new user.          |\n| `POST`     | `/api/auth/login`    | Login and obtain a JWT token. |\n\n---\n\n### **User Routes**\n\n| **Method** | **Endpoint**     | **Description**                                  |\n| ---------- | ---------------- | ------------------------------------------------ |\n| `GET`      | `/api/users`     | Retrieve a list of all users (Admin only).       |\n| `GET`      | `/api/users/:id` | Retrieve a user's profile (Admin or themselves). |\n\n---\n\n### **Blog Routes**\n\n| **Method** | **Endpoint**     | **Description**                                           |\n| ---------- | ---------------- | --------------------------------------------------------- |\n| `GET`      | `/api/blogs`     | Retrieve all blogs (with search, filter, and pagination). |\n| `POST`     | `/api/blogs`     | Create a new blog post (User only).                       |\n| `GET`      | `/api/blogs/:id` | Retrieve a single blog by its ID.                         |\n| `PUT`      | `/api/blogs/:id` | Update a blog post (User can update their own blogs).     |\n| `DELETE`   | `/api/blogs/:id` | Delete a blog post (User can delete their own blogs).     |\n\n---\n\n### **Admin Routes**\n\n| **Method** | **Endpoint**                     | **Description**               |\n| ---------- | -------------------------------- | ----------------------------- |\n| `POST`     | `/api/admin/users/:userId/block` | Block a user (Admin only).    |\n| `DELETE`   | `/api/admin/blogs/:id`           | Delete any blog (Admin only). |\n\n---\n\n### Authentication\n\n#### 1.1 Register User\n\n**POST** `/api/auth/register`\n\nDescription: Registers a new user with the platform. It validates user data and saves it to the database.\n\n**Request Body**:\n\n```json\n{\n  \"name\": \"John Doe\",\n  \"email\": \"john@example.com\",\n  \"password\": \"securepassword\"\n}\n```\n\n**Response**:\n\nSuccess (201):\n\n```json\n{\n  \"success\": true,\n  \"message\": \"User registered successfully\",\n  \"statusCode\": 201,\n  \"data\": {\n    \"_id\": \"string\",\n    \"name\": \"string\",\n    \"email\": \"string\"\n  }\n}\n```\n\nFailure (400):\n\n```json\n{\n  \"success\": false,\n  \"message\": \"Validation error\",\n  \"statusCode\": 400,\n  \"error\": {\n    \"details\": \"Field validation failed\"\n  },\n  \"stack\": \"error stack\"\n}\n```\n\n#### 1.2 Login User\n\n**POST** `/api/auth/login`\n\nDescription: Authenticates a user and returns a JWT token.\n\n**Request Body**:\n\n```json\n{\n  \"email\": \"john@example.com\",\n  \"password\": \"securepassword\"\n}\n```\n\n**Response**:\n\nSuccess (200):\n\n```json\n{\n  \"success\": true,\n  \"message\": \"Login successful\",\n  \"statusCode\": 200,\n  \"data\": {\n    \"token\": \"jwt_token_here\"\n  }\n}\n```\n\nFailure (401):\n\n```json\n{\n  \"success\": false,\n  \"message\": \"Invalid credentials\",\n  \"statusCode\": 401\n}\n```\n\n### User Management\n\n#### 2.1 Get All Users (Admin Only)\n\n**GET** `/api/users`\n\nDescription: Retrieves a list of all users in the platform (Admin only).\n\n**Response**:\n\nSuccess (200):\n\n```json\n{\n  \"success\": true,\n  \"data\": [\n    {\n      \"_id\": \"user_id\",\n      \"name\": \"John Doe\",\n      \"email\": \"john@example.com\",\n      \"role\": \"user\"\n    }\n  ]\n}\n```\n\n#### 2.2 Get User Profile\n\n**GET** `/api/users/:id`\n\nDescription: Retrieves a user’s profile (Admin or the user themselves).\n\n**Response**:\n\nSuccess (200):\n\n```json\n{\n  \"success\": true,\n  \"data\": {\n    \"_id\": \"user_id\",\n    \"name\": \"John Doe\",\n    \"email\": \"john@example.com\",\n    \"role\": \"user\",\n    \"isBlocked\": false\n  }\n}\n```\n\n#### 2.3 Update User Profile\n\n**PUT** `/api/users/:id`\n\nDescription: Updates a user’s profile (Admin or the user themselves).\n\n**Request Body**:\n\n```json\n{\n  \"name\": \"Updated Name\",\n  \"email\": \"newemail@example.com\"\n}\n```\n\n**Response**:\n\nSuccess (200):\n\n```json\n{\n  \"success\": true,\n  \"message\": \"Profile updated successfully\"\n}\n```\n\n### Blog Management\n\n#### 3.1 Get All Blogs\n\n**GET** `/api/blogs`\n\nDescription: Retrieves all blog posts, with support for search, filtering, and pagination.\n\n**Response**:\n\nSuccess (200):\n\n```json\n{\n  \"success\": true,\n  \"data\": [\n    {\n      \"_id\": \"blog_id\",\n      \"title\": \"Blog Title\",\n      \"content\": \"Blog content here...\",\n      \"author\": \"author_id\",\n      \"isPublished\": true,\n      \"createdAt\": \"2021-10-01T00:00:00.000Z\"\n    }\n  ]\n}\n```\n\n#### 3.2 Create Blog\n\n**POST** `/api/blogs`\n\nDescription: Allows the user to create a new blog post.\n\n**Request Body**:\n\n```json\n{\n  \"title\": \"Blog Title\",\n  \"content\": \"Content of the blog post\",\n  \"author\": \"author_id\"\n}\n```\n\n**Response**:\n\nSuccess (201):\n\n```json\n{\n  \"success\": true,\n  \"message\": \"Blog created successfully\",\n  \"data\": {\n    \"_id\": \"new_blog_id\",\n    \"title\": \"Blog Title\",\n    \"content\": \"Content of the blog post\",\n    \"author\": \"author_id\"\n  }\n}\n```\n\n#### 3.3 Update Blog\n\n**PUT** `/api/blogs/:id`\n\nDescription: Allows the user to update their own blog post.\n\n**Request Body**:\n\n```json\n{\n  \"title\": \"Updated Blog Title\",\n  \"content\": \"Updated content of the blog post\"\n}\n```\n\n**Response**:\n\nSuccess (200):\n\n```json\n{\n  \"success\": true,\n  \"message\": \"Blog updated successfully\"\n}\n```\n\n#### 3.4 Delete Blog\n\n**DELETE** `/api/blogs/:id`\n\nDescription: Allows the user to delete their own blog post.\n\n**Response**:\n\nSuccess (200):\n\n```json\n{\n  \"success\": true,\n  \"message\": \"Blog deleted successfully\"\n}\n```\n\n### Admin Routes\n\n#### 4.1 Block User\n\n**POST** `/api/admin/users/:userId/block`\n\nDescription: Allows the admin to block a user by setting `isBlocked` to `true`.\n\n**Response**:\n\nSuccess (200):\n\n```json\n{\n  \"success\": true,\n  \"message\": \"User blocked successfully\"\n}\n```\n\n#### 4.2 Delete Blog (Admin Only)\n\n**DELETE** `/api/admin/blogs/:id`\n\nDescription: Allows the admin to delete any blog post.\n\n**Response**:\n\nSuccess (200):\n\n```json\n{\n  \"success\": true,\n  \"message\": \"Blog deleted successfully\"\n}\n```\n\n## Error Handling\n\nThe application implements robust error handling mechanisms to ensure a smooth user experience and facilitate debugging for developers. Below is a list of standardized error types:\n\n| **Error Type**            | **Description**                                                                                     |\n| ------------------------- | --------------------------------------------------------------------------------------------------- |\n| **Zod Validation Error**  | **(ZOD_ERROR)**: Errors arising from invalid data inputs based on Zod schema validation.            |\n| **Not Found Error**       | **(NOT_FOUND_ERROR)**: When requested resources (e.g., a user, item, or page) are not found.        |\n| **Validation Error**      | **(VALIDATION_ERROR)**: General validation errors (e.g., incorrect data format, missing fields).    |\n| **Authentication Error**  | **(AUTH_ERROR)**: Issues related to failed authentication (e.g., invalid token or expired session). |\n| **Authorization Error**   | **(AUTHORIZATION_ERROR)**: When the user lacks permissions to access a resource.                    |\n| **Internal Server Error** | **(INTERNAL_SERVER_ERROR)**: Unhandled errors or unexpected server issues.                          |\n\nThe global error handler is designed to catch all unhandled errors and send a clean and consistent response with a `500` status code for server-related issues.\n\n---\n\n## Authentication\n\n### JWT Authentication\n\n- The platform uses **JWT (JSON Web Tokens)** for authenticating users.\n- After successful registration or login, a JWT token is returned.\n- The token should be included in the **Authorization** header as `Bearer \u003ctoken\u003e` for protected routes.\n\n### Role-Based Authentication\n\n- **Admin** users can access all routes and manage blog posts and users.\n\n- **Regular** users can create, update, and delete their own blog posts but cannot manage other users.\n\n## Contribution Guidelines\n\nIf you'd like to contribute to this project, please fork the repository and submit a pull request. Ensure your changes are well-tested and follow the coding standards outlined in the project.\n\n## Contact\n\nFor questions or collaboration, please contact me via:\n\n- **Email**: [mrshakilhossain@outlook.com](mailto:mrshakilhossain@outlook.com)\n- **LinkedIn**: [https://www.linkedin.com/in/your-profile](https://www.linkedin.com/in/your-profile)\n\n- **Facebook**: [https://www.facebook.com/iamshakilhossain](https://www.facebook.com/iamshakilhossain)\n- **Portfolio**: [https://shakilhossain-sigma.vercel.app](https://shakilhossain-sigma.vercel.app)\n\n---\n\nCopyright © 2024 [Md Shakil Hossain](https://github.com/Shakilofficial).\u003cbr /\u003e\n\n## License\n\nThis project is [MIT](https://github.com/Shakilofficial/book-shop/blob/main/LICENSE) licensed.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fshakilofficial%2Fblog-platform-backend","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fshakilofficial%2Fblog-platform-backend","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fshakilofficial%2Fblog-platform-backend/lists"}