This is a backend API for a blog application built with Node.js, Express, and MongoDB. The API supports user authentication, post creation, updating, deleting, liking, disliking, and commenting on posts.
- User Registration and Login with JWT-based authentication
- Create, read, update, and delete posts
- Like and dislike posts
- Add, update, and delete comments on posts
- Get user profile information along with the post count
- Node.js
- Express.js
- MongoDB
- Mongoose
- JWT (JSON Web Token)
- bcrypt
- dotenv
- cookie-parser
- cors
- Node.js and npm installed
- MongoDB installed and running
- Clone the repository:
git clone https://github.com/shashankaz/blog_api.git
- Navigate to the project directory:
cd blog_api
- Install the dependencies:
npm install
- Create a
.env
file in the root directory and add the following environment variables:
PORT=3000
MONGO_URI=your_mongo_database_uri
JWT_SECRET=your_jwt_secret
- Start the server:
npm start
-
Register a new user
POST /api/auth/register
Request Body:
{ "name": "John Doe", "email": "[email protected]", "password": "password123", "bio": "Software Developer", "profilePicture": "url_to_profile_picture" }
-
Login a user
POST /api/auth/login
Request Body:
{ "email": "[email protected]", "password": "password123" }
-
Logout a user
GET /api/auth/logout
-
Get user profile and post count
GET /api/auth/profile
-
Create a new post
POST /api/posts
Request Body:
{ "title": "My First Post", "content": "This is the content of my first post." }
-
Get all posts
GET /api/posts
-
Get posts created by the logged-in user
GET /api/posts/me
-
Get a single post by ID
GET /api/posts/:id
-
Update a post
PUT /api/posts/:id
Request Body:
{ "title": "Updated Post Title", "content": "Updated content of the post." }
-
Delete a post
DELETE /api/posts/:id
-
Like a post
POST /api/posts/:id/like
-
Dislike a post
POST /api/posts/:id/dislike
-
Add a comment to a post
POST /api/posts/:id/comment
Request Body:
{ "content": "This is a comment." }
-
Update a comment
PUT /api/posts/:postId/comment/:commentId
Request Body:
{ "content": "Updated comment content." }
-
Delete a comment
DELETE /api/posts/:postId/comment/:commentId