Before diving into the implementation, let’s briefly discuss the concept of pagination. Pagination is the process of dividing a large set of data into smaller, more manageable pages. Each page contains a specific number of records, and clients can request specific pages using parameters like page number, page size, etc.
Now, let’s start implementing pagination in Express.js.
- Managing Request Parameters: To enable pagination, we need to handle request parameters from the client. Typically, two parameters are used:
page
: The page number to retrieve. This parameter is optional and defaults to 1 if not provided.pageSize
: The number of records to include per page. This parameter is optional and defaults to a predefined value if not provided.
You can extract these parameters from the request query:
const page = parseInt(req.query.page) || 1;
const pageSize = parseInt(req.query.pageSize) || 10;
- Calculating Offset and Limit:
Once we have the
page
andpageSize
values, we can calculate the necessary offset and limit values to retrieve the corresponding records from the database. The offset represents the number of records to skip before starting to retrieve, and the limit represents the number of records to retrieve.
const offset = (page - 1) * pageSize;
const limit = pageSize;
- Fetching Paginated Data:
Next, fetch the paginated data from the database using the calculated
offset
andlimit
values. This will vary depending on the data storage and access mechanism in your application.
const data = await YourModel.find()
.skip(offset)
.limit(limit);
- Including Pagination Metadata: To provide more information to the client, we should include pagination metadata in the API response. This metadata typically includes the total number of records, the current page, and the number of records per page.
const totalRecords = await YourModel.countDocuments();
const totalPages = Math.ceil(totalRecords / pageSize);
const response = {
data,
totalRecords,
totalPages,
currentPage: page,
pageSize
};
res.json(response);
Now, clients can make requests to your Express.js APIs using the page
and pageSize
parameters, and they will receive paginated data along with useful metadata.
Implementing pagination in Express.js APIs enhances performance, reduces network overhead, and provides a better user experience. By following the steps outlined above, you can easily add pagination functionality to your Express.js APIs. Happy coding!
#ExpressJS #Pagination