Stop 04 · REST API Design with ExpressTopics · 6/9

Pagination with Query Strings

Part of the MERN Stack Roadmap

In short

A shop with 20,000 products cannot send them all in one response. Pagination returns one small page at a time, which keeps the API fast and the phone's data bill small.

Returning thousands of records at once is slow, so APIs return one page at a time using query strings like ?page=2&limit=20, along with the total count for the UI.

The jaadu flips through a thick restaurant menu, holding one page open while the rest wait in a neat stack.

Think of it like this

Reading a menu card page by page

A big restaurant menu has many pages. You ask the waiter for page 2, and you can see at the bottom that there are 6 pages in all. ?page=2&limit=20 is that request, and the total count tells the React app how many page buttons to draw.

Watch it happen

page=2, limit=3 over nine products

P1
0
P2
1
P3
2
P4
3
P5
4
P6
5
P7
6
P8
7
P9
8

1/4Nine products sorted by newest first.

The big ideas

What you need to know

skip = (page - 1) x limit

Page numbers start at 1 for humans. Convert them to how many records to skip.

Validate and cap

Query values arrive as strings. Convert with Number, default bad values and cap limit (for example at 100).

Always sort

Without a stable sort order, items can repeat or vanish between pages.

Return metadata

Send { data, page, limit, total, totalPages } so React can draw page buttons.

See it in code

Walk through the code

js
1function parsePagination(query) {2  const page = Math.max(1, Number.parseInt(query.page, 10) || 1);3  const limit = Math.min(100, Math.max(1, Number.parseInt(query.limit, 10) || 20));4  return { page, limit, skip: (page - 1) * limit };5}6 7app.get("/api/products", async (req, res) => {8  const { page, limit, skip } = parsePagination(req.query);9 10  const [data, total] = await Promise.all([11    Product.find().sort({ createdAt: -1, _id: -1 }).skip(skip).limit(limit),12    Product.countDocuments(),13  ]);14 15  res.json({ data, page, limit, total, totalPages: Math.ceil(total / limit) });16});

1/5page defaults to 1 and can never be below 1.

GET /api/products?page=2&limit=20. The same pattern works with any data source.

Save yourself the pain

Common mistakes

Oops

Using req.query.limit directly, so ?limit=100000 returns the whole table.

Do this instead

Parse it to a number, give it a default and cap it at a sensible maximum.

Oops

Paginating without a sort.

Do this instead

Always sort on a stable field such as createdAt plus _id so pages stay consistent.

Your mission

Paginate a list endpoint

~25 min

Quick check

With limit=25, how many records should the server skip for page 4?

Go deeper

  1. 1Add page and limit query parameters to a list endpoint with safe defaults and a maximum limit.
  2. 2Return items plus total, page and totalPages in the response.
  3. 3Learn when cursor-based pagination works better than page numbers, such as for infinite feeds.

More topics at this stop

Stuck on this topic? Ask an Insider

Get 1:1 guidance from people who've walked this exact path — on the InsideEdge app.

Download
InsideEdge