This project is a RESTful Blog API built using Node.js, Express.js, MongoDB, and Mongoose.
It was created as part of my internship project at Syntecxhub.
- Create a new blog post
- Get all blog posts
- Get a single blog post by ID
- Update a blog post
- Delete a blog post
- Pagination using page number
- Default 5 blog posts per page
- Filter blog posts by tag
- Filter blog posts by author
- Filter blog posts by date
- Sort blog posts by newest or oldest
- Proper status codes and JSON responses
- Node.js
- Express.js
- MongoDB
- Mongoose
- Postman
blog-api
│
├── controllers
│ └── blogController.js
│
├── models
│ └── BlogPost.js
│
├── routes
│ └── blogRoutes.js
│
├── .env
├── server.js
├── package.json
└── README.mdClone the repository:
git clone https://github.com/your-username/blog-api.gitGo to the project folder:
cd blog-apiInstall dependencies:
npm installCreate a .env file in the root folder and add:
PORT=5000
MONGO_URI=mongodb://127.0.0.1:27017/blog_apiRun the project:
npm run devThe server will start on:
http://localhost:5000Each blog post contains:
{
"title": "Blog title",
"body": "Blog content",
"author": "Author name",
"tags": ["nodejs", "express", "mongodb"]
}| Method | Endpoint | Description |
|---|---|---|
| POST | /api/blogs |
Create a new blog post |
| GET | /api/blogs |
Get all blog posts |
| GET | /api/blogs/:id |
Get a single blog post by ID |
| PUT | /api/blogs/:id |
Update a blog post |
| DELETE | /api/blogs/:id |
Delete a blog post |
POST /api/blogsRequest body:
{
"title": "Introduction to Node.js",
"body": "Node.js is a JavaScript runtime used for backend development.",
"author": "John",
"tags": ["nodejs", "backend", "api"]
}GET /api/blogsGET /api/blogs/blog_id_herePUT /api/blogs/blog_id_hereRequest body:
{
"title": "Updated Blog Title",
"body": "Updated blog content",
"author": "John",
"tags": ["javascript", "express"]
}DELETE /api/blogs/blog_id_herePagination is handled using the page query parameter.
The default limit is 5 blog posts per page.
GET /api/blogs?page=1GET /api/blogs?page=2GET /api/blogs?page=3Pagination logic:
const limit = 5;
const skip = (page - 1) * limit;Example:
| Page | Limit | Skip |
|---|---|---|
| 1 | 5 | 0 |
| 2 | 5 | 5 |
| 3 | 5 | 10 |
GET /api/blogs?tag=nodejsGET /api/blogs?author=JohnGET /api/blogs?startDate=2026-05-01&endDate=2026-05-13GET /api/blogs?sort=newestGET /api/blogs?sort=oldestGET /api/blogs?page=1&tag=nodejs&author=John&sort=newestThis will return blog posts from page 1, written by John, containing the tag nodejs, sorted by newest first.
{
"success": true,
"message": "Blog posts fetched successfully",
"currentPage": 1,
"totalPages": 3,
"totalPosts": 15,
"postsPerPage": 5,
"data": [
{
"_id": "6644c8f23b9d9f8a12c12345",
"title": "Introduction to Node.js",
"body": "Node.js is used for backend development.",
"author": "John",
"tags": ["nodejs", "backend"],
"createdAt": "2026-05-13T10:30:00.000Z",
"updatedAt": "2026-05-13T10:30:00.000Z"
}
]
}{
"success": false,
"message": "Blog post not found"
}The API was tested using Postman.
Testing steps:
- Create multiple blog posts using the POST API
- Get all blog posts using the GET API
- Get a single blog post using its ID
- Update a blog post using the PUT API
- Delete a blog post using the DELETE API
- Test pagination using
page=1,page=2, andpage=3 - Test filtering by tag, author, and date
- Test sorting by newest and oldest
Vikas Kushwaha