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.
Key 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.
- User Registration & Login: JWT-based authentication for secure user login and token management.
- Role-Based Access Control: Differentiates between regular users and admin roles, with admins having more extensive permissions.
- CRUD Operations for Blogs: Allows users to create, update, delete, and read blog posts.
- Global Error Handling: Custom error classes to handle various types of errors such as validation, duplicate data, and MongoDB-specific errors.
- Zod Validation: Type-safe validation library for ensuring data integrity in API requests.
- Query Builder: Built-in support for filtering, searching, and paginating blog posts.
- Project Title
- Description
- Features
- Installation
- Usage
- API Endpoints
- Error Handling
- Authentication
- Contribution Guidelines
- Contact
- License
Before you begin, ensure you have the following installed:
- Node.js: Version 18 or higher
- npm: Node package manager
- MongoDB: Either a local instance or MongoDB Atlas for a cloud-based solution
-
Clone the repository to your local machine:
git clone https://github.com/Shakilofficial/blog-platform-backend.git cd blog-platform-backend
-
Install the necessary dependencies:
npm install
-
Set up your environment variables in a
.env
file. Example:PORT=5000 MONGO_URI=mongodb://localhost:27017/blog-platform JWT_SECRET=your_jwt_secret_key
-
Compile TypeScript:
npm run build
-
Start the server:
- For development:
npm run dev
- For production:
npm run start:prod
- For development:
Once the server is running, you can access the API at http://localhost:<port>/api
, where <port>
is defined in your .env
file (default is 5000
).
- Development Mode: The server will automatically reload with changes.
- Production Mode: The server will be optimized for performance.
Here’s a comprehensive list of the available API routes:
Method | Endpoint | Description |
---|---|---|
POST |
/api/auth/register |
Register a new user. |
POST |
/api/auth/login |
Login and obtain a JWT token. |
Method | Endpoint | Description |
---|---|---|
GET |
/api/users |
Retrieve a list of all users (Admin only). |
GET |
/api/users/:id |
Retrieve a user's profile (Admin or themselves). |
Method | Endpoint | Description |
---|---|---|
GET |
/api/blogs |
Retrieve all blogs (with search, filter, and pagination). |
POST |
/api/blogs |
Create a new blog post (User only). |
GET |
/api/blogs/:id |
Retrieve a single blog by its ID. |
PUT |
/api/blogs/:id |
Update a blog post (User can update their own blogs). |
DELETE |
/api/blogs/:id |
Delete a blog post (User can delete their own blogs). |
Method | Endpoint | Description |
---|---|---|
POST |
/api/admin/users/:userId/block |
Block a user (Admin only). |
DELETE |
/api/admin/blogs/:id |
Delete any blog (Admin only). |
POST /api/auth/register
Description: Registers a new user with the platform. It validates user data and saves it to the database.
Request Body:
{
"name": "John Doe",
"email": "john@example.com",
"password": "securepassword"
}
Response:
Success (201):
{
"success": true,
"message": "User registered successfully",
"statusCode": 201,
"data": {
"_id": "string",
"name": "string",
"email": "string"
}
}
Failure (400):
{
"success": false,
"message": "Validation error",
"statusCode": 400,
"error": {
"details": "Field validation failed"
},
"stack": "error stack"
}
POST /api/auth/login
Description: Authenticates a user and returns a JWT token.
Request Body:
{
"email": "john@example.com",
"password": "securepassword"
}
Response:
Success (200):
{
"success": true,
"message": "Login successful",
"statusCode": 200,
"data": {
"token": "jwt_token_here"
}
}
Failure (401):
{
"success": false,
"message": "Invalid credentials",
"statusCode": 401
}
GET /api/users
Description: Retrieves a list of all users in the platform (Admin only).
Response:
Success (200):
{
"success": true,
"data": [
{
"_id": "user_id",
"name": "John Doe",
"email": "john@example.com",
"role": "user"
}
]
}
GET /api/users/:id
Description: Retrieves a user’s profile (Admin or the user themselves).
Response:
Success (200):
{
"success": true,
"data": {
"_id": "user_id",
"name": "John Doe",
"email": "john@example.com",
"role": "user",
"isBlocked": false
}
}
PUT /api/users/:id
Description: Updates a user’s profile (Admin or the user themselves).
Request Body:
{
"name": "Updated Name",
"email": "newemail@example.com"
}
Response:
Success (200):
{
"success": true,
"message": "Profile updated successfully"
}
GET /api/blogs
Description: Retrieves all blog posts, with support for search, filtering, and pagination.
Response:
Success (200):
{
"success": true,
"data": [
{
"_id": "blog_id",
"title": "Blog Title",
"content": "Blog content here...",
"author": "author_id",
"isPublished": true,
"createdAt": "2021-10-01T00:00:00.000Z"
}
]
}
POST /api/blogs
Description: Allows the user to create a new blog post.
Request Body:
{
"title": "Blog Title",
"content": "Content of the blog post",
"author": "author_id"
}
Response:
Success (201):
{
"success": true,
"message": "Blog created successfully",
"data": {
"_id": "new_blog_id",
"title": "Blog Title",
"content": "Content of the blog post",
"author": "author_id"
}
}
PUT /api/blogs/:id
Description: Allows the user to update their own blog post.
Request Body:
{
"title": "Updated Blog Title",
"content": "Updated content of the blog post"
}
Response:
Success (200):
{
"success": true,
"message": "Blog updated successfully"
}
DELETE /api/blogs/:id
Description: Allows the user to delete their own blog post.
Response:
Success (200):
{
"success": true,
"message": "Blog deleted successfully"
}
POST /api/admin/users/:userId/block
Description: Allows the admin to block a user by setting isBlocked
to true
.
Response:
Success (200):
{
"success": true,
"message": "User blocked successfully"
}
DELETE /api/admin/blogs/:id
Description: Allows the admin to delete any blog post.
Response:
Success (200):
{
"success": true,
"message": "Blog deleted successfully"
}
The 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:
Error Type | Description |
---|---|
Zod Validation Error | (ZOD_ERROR): Errors arising from invalid data inputs based on Zod schema validation. |
Not Found Error | (NOT_FOUND_ERROR): When requested resources (e.g., a user, item, or page) are not found. |
Validation Error | (VALIDATION_ERROR): General validation errors (e.g., incorrect data format, missing fields). |
Authentication Error | (AUTH_ERROR): Issues related to failed authentication (e.g., invalid token or expired session). |
Authorization Error | (AUTHORIZATION_ERROR): When the user lacks permissions to access a resource. |
Internal Server Error | (INTERNAL_SERVER_ERROR): Unhandled errors or unexpected server issues. |
The 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.
- The platform uses JWT (JSON Web Tokens) for authenticating users.
- After successful registration or login, a JWT token is returned.
- The token should be included in the Authorization header as
Bearer <token>
for protected routes.
-
Admin users can access all routes and manage blog posts and users.
-
Regular users can create, update, and delete their own blog posts but cannot manage other users.
If 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.
For questions or collaboration, please contact me via:
-
Email: mrshakilhossain@outlook.com
-
LinkedIn: https://www.linkedin.com/in/your-profile
-
Portfolio: https://shakilhossain-sigma.vercel.app
Copyright © 2024 Md Shakil Hossain.
This project is MIT licensed.