This project implements a backend service for a blog platform, allowing users to perform CRUD operations on blog posts and manage user accounts. The backend is built using Node.js with the Express.js framework and uses MongoDB for data storage. Authentication is handled using token-based authentication (JWT).
- Backend Framework: Express.js (Node.js)
- Database: MongoDB
- Authentication: JSON Web Tokens (JWT)
- API Endpoints:
- Create a new blog post
- Retrieve a list of all blog posts
- Retrieve a single blog post by its ID
- Retrieve blog posts by user ID
- Update an existing blog post
- Delete a blog post
- Create, update, delete, and retrieve user details
- Data Validation:
- Validate input data to ensure it meets the specified requirements (e.g., title and content are required fields).
- Authentication:
- Secure the API endpoints using token-based authentication.
- Users must authenticate before performing any CRUD operations on blog posts and user details.
-
Clone the repository:
git clone https://github.com/Deepika0701/Blog-Backend.git cd Blog-Backend
-
Install dependencies:
npm install
-
Set up the MongoDB database and update the
DATABASE_URL
in.env
file:MONGODB_URL={Write your url}
-
Start the server:
npm start
-
Update User
- URL:
/api/users/:id
- Method:
PUT
- Headers:
{ "Authorization": "Bearer your_jwt_token" }
- Request Body (Example):
{ "username": "newUsername", "password": "newPassword" }
- Response:
{ "id": "user_id", "username": "newUsername", ... }
- URL:
-
Delete User
- URL:
/api/users/:id
- Method:
DELETE
- Headers:
{ "Authorization": "Bearer your_jwt_token" }
- Response:
{ "message": "User has been deleted successfully" }
- URL:
-
Get User
- URL:
/api/users/:id
- Method:
GET
- Response:
{ "id": "user_id", "username": "username", ... }
- URL:
-
Create a New Blog Post
- URL:
/api/posts/create
- Method:
POST
- Headers:
{ "Authorization": "Bearer your_jwt_token" }
- Request Body:
{ "title": "Blog Post Title", "content": "This is the content of the blog post.", "userId": "user_id" }
- Response:
{ "id": "post_id", "title": "Blog Post Title", "content": "This is the content of the blog post.", "userId": "user_id", "createdAt": "2024-06-15T00:00:00.000Z", "updatedAt": "2024-06-15T00:00:00.000Z" }
- URL:
-
Update an Existing Blog Post
- URL:
/api/posts/:id
- Method:
PUT
- Headers:
{ "Authorization": "Bearer your_jwt_token" }
- Request Body:
{ "title": "Updated Blog Post Title", "content": "This is the updated content of the blog post." }
- Response:
{ "id": "post_id", "title": "Updated Blog Post Title", "content": "This is the updated content of the blog post.", "userId": "user_id", "createdAt": "2024-06-15T00:00:00.000Z", "updatedAt": "2024-06-15T00:00:00.000Z" }
- URL:
-
Delete a Blog Post
- URL:
/api/posts/:id
- Method:
DELETE
- Headers:
{ "Authorization": "Bearer your_jwt_token" }
- Response:
{ "message": "Post has been deleted successfully" }
- URL:
-
Get Post Details
- URL:
/api/posts/:id
- Method:
GET
- Response:
{ "id": "post_id", "title": "Blog Post Title", "content": "This is the content of the blog post.", "userId": "user_id", "createdAt": "2024-06-15T00:00:00.000Z", "updatedAt": "2024-06-15T00:00:00.000Z" }
- URL:
-
Get All Posts
- URL:
/api/posts
- Method:
GET
- Query Parameters (Optional):
search
: Search term to filter posts by title
- Response:
[ { "id": "post_id", "title": "Blog Post Title", "content": "This is the content of the blog post.", "userId": "user_id", "createdAt": "2024-06-15T00:00:00.000Z", "updatedAt": "2024-06-15T00:00:00.000Z" }, ... ]
- URL:
-
Get Posts by User ID
- URL:
/api/posts/user/:userId
- Method:
GET
- Response:
[ { "id": "post_id", "title": "Blog Post Title", "content": "This is the content of the blog post.", "userId": "user_id", "createdAt": "2024-06-15T00:00:00.000Z", "updatedAt": "2024-06-15T00:00:00.000Z" }, ... ]
- URL:
- Ensure that the
title
andcontent
fields are present in the request body when creating or updating a blog post. - Return a
400 Bad Request
status code with an appropriate error message if the validation fails.
- Implement token-based authentication using JWT.
- Protect all CRUD operations on blog posts and user details by requiring a valid JWT in the
Authorization
header.