Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Node pagination with express

I have to build and API as server side, that should provide results in pages of 10 entries, using only Node with express (no other packages).

  • A query parameter p specifies which page to return, starting with 1. If the p parameter is omitted, the default value is 1.
  • If the client side asks for /api/stories?p=1, they should only get 10 stories, starting from the newest one.

  • If p=2, the API must return the second batch of 10 stories. When a page of stories is returned it must be ordered with the most recent story first.

  • If p is greater than the last page number, the API must return the last available page.

  • The page value is the currently returned page. If the requested page p is greater than the last page number, the returned page value will indicate the last page number.

  • The pageCount value is the number of the last non-empty page.*

This is what I have for pagination...

//pagination
const pageLimit = 10;
app.get('/api/posts', function(req, res) {
  res.json({
    "posts": posts.slice(-pageLimit).reverse(),
    "page": 1,
    "pageCount": Math.ceil(posts.length / 10)
  });
});

The return is correct as 10 posts per page, 11th, 21st, 31st posts are pushed on 2nd ,3rd page etc... Now my problem comes when I try to advance from page 1 to page 2 (as a next page) nothing happens...

I'm guessing I have to implement something like a a next but I don't have any idea how to do it

Any help would be appreciated...

like image 722
Gallex Avatar asked Dec 13 '17 18:12

Gallex


People also ask

What is pagination in mongoose?

It is a traditional way to paginate with mongoose. Here, It simply uses limit and offset in SQL queries to paginate the data from database. If you are working on NOSQL database, it will be limit and skip. In this approach, client will supply two query parameters in the request: page and limit.

What is offset and limit in API?

Offset & LimitThe maximum number of entries to return. If the value exceeds the maximum, then the maximum value will be used. The maximum offset for offset-based pagination is 9999 . Marker-based pagination is recommended when a higher offset is needed.

How does JavaScript pagination work?

JavaScript Pagination concept is applied for moving among the pages with First, Next, Previous and Last buttons or links. Pagination's main motto is to move among the content immediately by clicking links or buttons. Pagination has multiple links or buttons provided for access First, Next, Previous and Last content.


2 Answers

after scratching my head 2 days, I found a solution that works for me..Thank you num8er for pointing me in right direction...

app.get('/api/posts', (req, res) => {
  const pageCount = Math.ceil(posts.length / 10);
  let page = parseInt(req.query.p);
  if (!page) { page = 1;}
  if (page > pageCount) {
    page = pageCount
  }
  res.json({
    "page": page,
    "pageCount": pageCount,
    "posts": posts.slice(page * 10 - 10, page * 10)
  });
});
like image 149
Gallex Avatar answered Oct 03 '22 08:10

Gallex


Check this out:

app.get('/api/posts', (req, res) => {
  const postCount = posts.length;
  const perPage = 10;
  const pageCount = Math.ceil(postCount / perPage);

  let page = parseInt(req.query.p);
  if(page < 1) page = 1;
  if(page > pageCount) page = pageCount;

  const from = postCount - ((page - 1) * perPage) - 1; // ex.: 44 - ((1 - 1) * 10) -1 = 43 (44 is count, 43 is index)
  let to = postCount - (page * perPage); // ex.: 44 - (1 * 10) = 34
  if(to < 0) to = 0;

  res.json({
    posts: posts.slice(from, to).reverse(),
    page,
    pageCount
  });
});

P.S. If posts array retrieved from database - I strongly recommend to use database powers to retrieve necessary data. Otherwise retrieving thousands of page and then slicing array to 10 items is will result with performance issues.

like image 29
num8er Avatar answered Oct 03 '22 09:10

num8er