A simple blog API built using Node.js, Express, and MongoDB. This project allows you to create, read, update, delete, and search blog posts.
https://roadmap.sh/projects/blogging-platform-api
- Introduction
- Features
- Technologies Used
- Installation
- Configuration
- Usage
- API Endpoints
- Examples
- Troubleshooting
- Contributors
- License
This project is a basic RESTful API for managing blog posts. It supports operations such as creating, retrieving, updating, deleting, and searching for blog posts. It uses MongoDB for data storage and Express as the server framework.
- Add new blog posts
- Get all blog posts
- Get a single blog post by ID
- Update a blog post
- Delete a blog post
- Search blog posts by title, content, or category
- Timestamps for posts (createdAt, updatedAt)
- Node.js
- Express.js
- MongoDB
- Mongoose
- dotenv for environment variables
- body-parser for handling request bodies
-
Clone the repository:
git clone https://github.com/Rahul-Rasal/blog-api.git
-
Navigate to the project directory:
cd blog-api -
Install the dependencies:
npm install
-
Set up the environment variables by creating a
.envfile in the root directory and adding the following:MONGODB_URI=your-mongodb-uri PORT=your-port
Replace
your-mongodb-uriwith your actual MongoDB connection string andyour-portwith the desired port number.
- MongoDB: Make sure you have a MongoDB instance running and configure the connection string in the
.envfile. - Port: You can specify a custom port in the
.envfile. If not provided, the server will default to port2000.
-
Start the server:
npm start
-
The API will run at
http://localhost:2000(or the port you configured).
-
URL:
/addpost -
Method:
POST -
Body:
{ "title": "Your blog title", "content": "Your blog content", "category": "Your category", "tags": ["tag1", "tag2"] }
- URL:
/allposts - Method:
GET
- URL:
/:id - Method:
GET
-
URL:
/:id -
Method:
POST -
Body:
{ "title": "Updated title", "content": "Updated content", "category": "Updated category", "tags": ["updatedTag1", "updatedTag2"] }
- URL:
/:id - Method:
DELETE
- URL:
/search?term=your-search-term - Method:
GET
-
Create a post:
curl -X POST http://localhost:2000/addpost -H "Content-Type: application/json" -d '{"title": "Sample Title", "content": "Sample Content", "category": "Tech", "tags": ["node", "express"]}'
-
Get all posts:
curl http://localhost:2000/allposts
-
Search for posts:
curl http://localhost:2000/search?term=Tech
- MongoDB Connection Error: If the server fails to connect to MongoDB, make sure the MongoDB URI in the
.envfile is correct, and that your MongoDB instance is running. - Port Already in Use: If the port is already in use, change the port in the
.envfile or terminate the process using that port.
This project is licensed under the MIT License. See the LICENSE file for details.