A simple REST API built with Express.js and MongoDB for managing blog posts. This project demonstrates CRUD operations using Mongoose ODM.
- Create Posts: Add new blog posts with title and content
- Read Posts: Retrieve all posts from the database
- Update Posts: Modify existing posts by ID
- Delete Posts: Remove posts from the database
- MongoDB Integration: Uses Mongoose for database operations
- Error Handling: Comprehensive error handling for all endpoints
Before running this project, make sure you have:
- Node.js (v14 or higher)
- MongoDB Atlas account (or local MongoDB instance)
- npm or yarn package manager
-
Clone the repository
git clone <repository-url> cd database_task
-
Install dependencies
npm install
-
Configure MongoDB connection
- Update the MongoDB connection string in
index.js(line 13) - Replace
<username>and<password>with your MongoDB Atlas credentials - Or use your local MongoDB connection string
- Update the MongoDB connection string in
-
Start the server
npm start
The server will start on http://localhost:3000
- GET
/test- Returns: "Hello Worldd"
- Purpose: Basic connectivity test
- POST
/addPosts - Body (JSON):
{ "title": "Your Post Title", "content": "Your post content here" } - Response: Success message with created post data
- GET
/getPosts - Response: Array of all posts in the database
- PATCH
/post/:id - Body (JSON):
{ "title": "Updated Title", "content": "Updated content" } - Response: Success message with updated post data
- DELETE
/delPosts/:id - Response: Success message confirming deletion
The project uses a simple Post schema:
{
title: String,
content: String
}You can test the API using:
- Postman - Import the collection or manually test endpoints
- MongoDB Compass - View database changes
- curl commands:
# Test connection
curl http://localhost:3000/test
# Create a post
curl -X POST http://localhost:3000/addPosts \
-H "Content-Type: application/json" \
-d '{"title":"My First Post","content":"This is my first blog post"}'
# Get all posts
curl http://localhost:3000/getPosts
# Update a post (replace :id with actual post ID)
curl -X PATCH http://localhost:3000/post/:id \
-H "Content-Type: application/json" \
-d '{"title":"Updated Title","content":"Updated content"}'
# Delete a post (replace :id with actual post ID)
curl -X DELETE http://localhost:3000/delPosts/:iddatabase_task/
├── index.js # Main server file
├── package.json # Project dependencies
├── package-lock.json # Dependency lock file
├── node_modules/ # Installed packages
├── screenshots/ # API testing screenshots
│ ├── add_post_compass.png.png
│ ├── add_post_postman.png.png
│ ├── delete_post_compass.png.png
│ ├── delete_post_postman.png.png
│ ├── get_posts_postman.png.png
│ ├── patch_post_compass.png.png
│ └── patch_post_postman.png.png
└── README.md # This file
Update the connection string in index.js:
await mongoose.connect("mongodb+srv://<username>:<password>.ypxjvwp.mongodb.net/inventory");The API includes comprehensive error handling:
- 400 Bad Request: Invalid request data
- 404 Not Found: Post not found (for update/delete operations)
- 500 Internal Server Error: Server-side errors
The screenshots/ folder contains visual documentation of API testing using:
- MongoDB Compass (database operations)
- Postman (API endpoint testing)
.
For issues and questions:
- Check the error logs in the console
- Verify your MongoDB connection
- Ensure all dependencies are installed correctly