title: Node pagination with express
body: <p>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).   </p>
  
  <ul>
  <li>A query parameter <code>p</code> specifies which page to return, starting with 1. If the <code>p</code> parameter is omitted, the default value is 1.</li>
  <li><p>If the client side asks for <code>/api/stories?p=1</code>, they should only get 10 stories, starting from the newest one.</p></li>
  <li><p>If <code>p=2</code>, 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.</p></li>
  <li><p>If <code>p</code> is greater than the last page number, the API must return the last available page.</p></li>
  <li><p>The <code>page</code> value is the currently returned page. If the requested page <code>p</code> is greater than the last page number, the returned <code>page</code> value will indicate the last page number.</p></li>
  <li>The <code>pageCount</code> value is the number of the last non-empty page.*</li>
  </ul>
  
  <p>This is what I have for pagination...</p>
  
  <pre><code>//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)
    });
  });
  </code></pre>
  
  <p>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...</p>
  
  <p>I'm guessing I have to implement something like a a next but I don't have any idea how to do it</p>
  
  <p>Any help would be appreciated...</p>