0
0
Node.jsframework~10 mins

Pagination patterns in Node.js - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Pagination patterns
Start Request with page & limit
Calculate offset = (page-1)*limit
Query database with offset & limit
Receive subset of data
Send data + pagination info
Client displays current page data
User clicks next/prev page
Back to Start Request
This flow shows how pagination works by calculating which data slice to fetch and then displaying it, looping as user navigates pages.
Execution Sample
Node.js
const page = 2;
const limit = 3;
const offset = (page - 1) * limit;
const data = allItems.slice(offset, offset + limit);
console.log(data);
This code fetches the second page of data with 3 items per page from a list.
Execution Table
StepVariableValueActionOutput
1page2Set current page
2limit3Set items per page
3offset(2-1)*3=3Calculate offset
4dataallItems.slice(3,6)Slice data from index 3 to 5[item4, item5, item6]
5console.logPrint data[item4, item5, item6]
6--End of pagination fetchNo more steps
💡 All steps done, data for page 2 with limit 3 fetched and displayed
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3After Step 4Final
pageundefined22222
limitundefinedundefined3333
offsetundefinedundefinedundefined333
dataundefinedundefinedundefinedundefined[item4, item5, item6][item4, item5, item6]
Key Moments - 3 Insights
Why do we calculate offset as (page - 1) * limit?
Because pages start at 1, but array indexes start at 0, so offset shifts to correct start index. See execution_table step 3.
What happens if page is 1?
Offset becomes 0, so data slice starts from the beginning. This is shown by the formula in step 3.
Why do we slice from offset to offset + limit?
To get exactly 'limit' number of items starting at offset. See step 4 where slice(3,6) gets 3 items.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the value of offset at step 3?
A3
B2
C6
D0
💡 Hint
Check the 'Value' column in step 3 of execution_table
At which step is the data slice created?
AStep 2
BStep 4
CStep 3
DStep 5
💡 Hint
Look for the action 'Slice data' in execution_table
If limit changes to 5, how does the slice range change at step 4?
Aslice(3,6)
Bslice(2,7)
Cslice(3,8)
Dslice(0,5)
💡 Hint
Offset stays 3, slice ends at offset + limit, see variable_tracker for limit
Concept Snapshot
Pagination fetches a subset of data by:
1. Setting page number and items per page (limit)
2. Calculating offset = (page-1)*limit
3. Slicing data from offset to offset+limit
4. Returning this slice for display
This pattern helps load data in chunks for better performance.
Full Transcript
Pagination patterns in Node.js involve calculating which part of a data list to show based on the current page and how many items per page. We start by setting the page number and limit. Then we calculate the offset by multiplying (page minus one) by the limit. This offset tells us where to start slicing the data array. We slice from offset to offset plus limit to get the current page's items. Finally, we send this slice to the client to display. This process repeats as the user navigates pages, fetching only the needed data each time.