Skip to content

Latest commit

 

History

1 Commit

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Blog API with Pagination and Filtering

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.

Features

  • 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

Technologies Used

  • Node.js
  • Express.js
  • MongoDB
  • Mongoose
  • Postman

Project Structure

blog-api
│
├── controllers
│   └── blogController.js
│
├── models
│   └── BlogPost.js
│
├── routes
│   └── blogRoutes.js
│
├── .env
├── server.js
├── package.json
└── README.md

Installation

Clone the repository:

git clone https://github.com/your-username/blog-api.git

Go to the project folder:

cd blog-api

Install dependencies:

npm install

Create a .env file in the root folder and add:

PORT=5000
MONGO_URI=mongodb://127.0.0.1:27017/blog_api

Run the project:

npm run dev

The server will start on:

http://localhost:5000

Blog Post Model

Each blog post contains:

{
  "title": "Blog title",
  "body": "Blog content",
  "author": "Author name",
  "tags": ["nodejs", "express", "mongodb"]
}

API Endpoints

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

Create Blog Post

POST /api/blogs

Request body:

{
  "title": "Introduction to Node.js",
  "body": "Node.js is a JavaScript runtime used for backend development.",
  "author": "John",
  "tags": ["nodejs", "backend", "api"]
}

Get All Blog Posts

GET /api/blogs

Get Single Blog Post by ID

GET /api/blogs/blog_id_here

Update Blog Post

PUT /api/blogs/blog_id_here

Request body:

{
  "title": "Updated Blog Title",
  "body": "Updated blog content",
  "author": "John",
  "tags": ["javascript", "express"]
}

Delete Blog Post

DELETE /api/blogs/blog_id_here

Pagination

Pagination is handled using the page query parameter.

The default limit is 5 blog posts per page.

Page 1

GET /api/blogs?page=1

Page 2

GET /api/blogs?page=2

Page 3

GET /api/blogs?page=3

Pagination logic:

const limit = 5;
const skip = (page - 1) * limit;

Example:

Page Limit Skip
1 5 0
2 5 5
3 5 10

Filtering

Filter by Tag

GET /api/blogs?tag=nodejs

Filter by Author

GET /api/blogs?author=John

Filter by Date

GET /api/blogs?startDate=2026-05-01&endDate=2026-05-13

Sorting

Newest First

GET /api/blogs?sort=newest

Oldest First

GET /api/blogs?sort=oldest

Combined Query Example

GET /api/blogs?page=1&tag=nodejs&author=John&sort=newest

This will return blog posts from page 1, written by John, containing the tag nodejs, sorted by newest first.

Sample Success Response

{
  "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"
    }
  ]
}

Error Response Example

{
  "success": false,
  "message": "Blog post not found"
}

Testing

The API was tested using Postman.

Testing steps:

  1. Create multiple blog posts using the POST API
  2. Get all blog posts using the GET API
  3. Get a single blog post using its ID
  4. Update a blog post using the PUT API
  5. Delete a blog post using the DELETE API
  6. Test pagination using page=1, page=2, and page=3
  7. Test filtering by tag, author, and date
  8. Test sorting by newest and oldest

Author

Vikas Kushwaha

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages