Deviopath › Node.js

Paginate a List Endpoint

Medium · HTTP

A GET /items?page=2&limit=10 handler reads page and limit from req.query, and query values are always strings (or missing). Write paginate(items, query) that returns one page of items.

- page: a positive whole number written with digits only (like "3"). Missing or invalid ("abc", "0", "-1", "2.5") → 1.

- limit: same rules, default 10. Values above 100 are clamped to 100.

- A page past the end returns an empty data array (it is not an error).

Return { data, meta: { page, limit, total, totalPages, hasNext, hasPrev } } where total is items.length, totalPages is Math.ceil(total / limit), hasNext is page < totalPages and hasPrev is page > 1.

Next: Validate a Signup Body