A RESTful API for managing blog posts and comments, allowing users to create, read, update, and delete blog posts and manage comments on posts. Built with Node.js, Express, MongoDB, and JWT for secure authentication.
- User authentication with JWT
- CRUD operations for blog posts
- Commenting system for blog posts
- Modular and scalable code structure
- Middleware for error handling and authentication
- Node.js: Server-side JavaScript runtime
- Express: Web framework for building RESTful APIs
- MongoDB: NoSQL database for data storage
- JWT: JSON Web Tokens for secure user authentication
-
Create a MongoDB Atlas Account:
- Go to MongoDB Atlas and sign up or log in.
- After logging in, click Build a Database.
-
Create a Cluster:
- Select a cloud provider and region for your cluster (choose the free tier for a free database).
- Configure your cluster settings as needed and click Create Cluster.
-
Create a Database User:
- Navigate to Database Access in the left sidebar.
- Click Add New Database User.
- Set up a username and password for this user and ensure they have read and write access.
- Note the username and password; you’ll use these in your connection string.
-
Configure Network Access:
- Go to Network Access in the left sidebar.
- Click Add IP Address and either:
- Click Allow Access from Anywhere (suitable for development) or
- Add your IP address if you prefer restricted access.
- Save changes to enable access.
-
Get Your Connection String:
- Go to Clusters in the left sidebar, and click Connect for your cluster.
- Choose Connect your application and copy the connection string.
- Replace
<username>
and<password>
with your database user's credentials. - Replace
<dbname>
with the name of your database.
-
Clone the repository:
git clone https://github.com/md-shahid-ansari/RESTful-Blog.git
-
Navigate to the project directory:
cd RESTful-Blog
-
Install the dependencies:
npm install
-
Create a
.env
file in the root directory and add the following environment variables:PORT=5000 MONGO_URI=your_mongodb_connection_string JWT_SECRET=your_jwt_secret_key NODE_ENV = development CLIENT_URL = http://localhost:3000 EMAIL = you_email EMAIL_PASSWORD = password for email (app)
-
Start the server:
npm start or npm run dev
The server will run on
http://localhost:5000
.
-
Endpoint:
POST /api/auth/register
-
Description: Register a new user
-
Request Body:
{ "username": "user123", "email": "user@example.com", "password": "yourpassword" }
-
Response:
{ "success": true, "message": "User registered successfully. Verification email sent.", "user":{...} }
and
-
Endpoint:
POST /api/auth/verify
-
Description: Verify email of a new user using code sent to the email
-
Request Body:
{ "code":"code_recieved_on_email" }
-
Response:
{ "success": true, "message": "User verified successfully", "user":{...} }
-
Endpoint:
POST /api/auth/login
-
Description: Login user and receive a JWT token
-
Request Body:
{ "email": "user@example.com", "password": "yourpassword" }
or
{ "username": "username", "password": "yourpassword" }
-
Response:
{ "success": true, "message": "User logged in successfully", "user": {...}, "userToken": "token_saved_in_the_cookie" }
-
Endpoint:
POST /api/auth/forgot
-
Description: Forgot password
-
Request Body:
{ "email": "user@example.com", }
or
{ "username": "username", }
-
Response:
{ "success": true, "message": "Reset token sent successfully on your registered email!" }
-
Endpoint:
POST /api/auth/reset
-
Description: Enter new password
-
Request Body:
{ "token":"token_recieved_on_email", "password":"yournewpassword" }
-
Response:
{ "success": true, "message": "Password changed successfully" }
-
Endpoint:
POST /api/auth/logout
-
Description: Logout, it wil delete userToken
-
Response:
{ "success": true, "message": "Logout successfully", "userToken": "token_deleted_from_the_cookie" }
-
Endpoint:
POST /api/posts
-
Description: Create a new blog post
-
Headers:
Authorization: Bearer your_jwt_token
-
Request Body:
{ "title": "My First Blog", "content": "This is the content of my first blog post.", }
-
Response:
{ "success": true, "post": {...} }
-
Endpoint:
GET /api/posts
-
Description: Retrieve a list of all blog posts
-
Response:
{ "success": true, "posts": [...] }
-
Endpoint:
GET /api/posts/:id
-
Description: Retrive single blog post
-
Headers:
Authorization: Bearer your_jwt_token
-
Response:
{ "success": true, "post": {...} }
-
Endpoint:
PUT /api/posts/:id
-
Description: Update an existing blog post
-
Headers:
Authorization: Bearer your_jwt_token
-
Request Body:
{ "title": "Updated Blog Title", "content": "Updated content" }
-
Response:
{ "success": true, "post": {...} }
-
Endpoint:
DELETE /api/posts/:id
-
Description: Delete a blog post
-
Headers:
Authorization: Bearer your_jwt_token
-
Response:
{ "message": "Post deleted successfully" }
-
Endpoint:
POST /api/comments
-
Description: Add a comment to a specific blog post
-
Headers:
Authorization: Bearer your_jwt_token
-
Request Body:
{ "post_id": "6732fc15c399f5ea87328059", "content": "This is a comment on the blog post." }
-
Response:
{ "success": true, "comment": {...} }
-
Endpoint:
GET /api/comments?post_id=post_id
-
Description: Retrive all comments of specific post
-
Headers:
Authorization: Bearer your_jwt_token
-
Response:
{ "success": true, "comments": [...] }
-
Endpoint:
GET /api/comments/:id
-
Description: read specific comment using comment id
-
Headers:
Authorization: Bearer your_jwt_token
-
Response:
{ "success": true, "comment": {...} }
-
Endpoint:
PUT /api/comments
-
Description: Add a comment to a specific blog post
-
Headers:
Authorization: Bearer your_jwt_token
-
Request Body:
{ "content": "This is an updated comment on a blog post." }
-
Response:
{ "success": true, "comment": {...} }
-
Endpoint:
DELETE /api/comments/:id
-
Description: Delete a specific comment from a blog post
-
Headers:
Authorization: Bearer your_jwt_token
-
Response:
{ "success": true, "message": "Comment deleted successfully" }
All error responses follow a consistent structure:
{
"error": "Error message explaining what went wrong"
}
This project was created during an internship.