Split JSON API's object into chunks in express app
<p>I want to separate my API's output into different pages. I want to call them like this: <code>http://127.0.0.1:3000/api/articles/0/&lt;API-TOKEN&gt;</code></p>

<p>Which is returning the first page with 2-3, etc. articles.</p>

<p>Full code can be found here: <a href="https://github.com/DJviolin/horgalleryNode/blob/master/routes/api.js" rel="nofollow noreferrer">https://github.com/DJviolin/horgalleryNode/blob/master/routes/api.js</a></p>

<p>I have dummy data JSON file:</p>

<pre><code>[
  {
    "articles": [
      {
        "id": "0",
        "url": "audrey-hepburn",
        "title": "Audrey Hepburn",
        "body": "Nothing is impossible, the word itself says 'I'm possible'!",
        "category": "foo",
        "tags": [ "foo" ]
      },
      {
        "id": "1",
        "url": "walt-disney",
        "title": "Walt Disney",
        "body": "You may not realize it when it happens, but a kick in the teeth may be the best thing in the world for you.",
        "category": "foo",
        "tags": [ "foo", "bar" ]
      },
      {
        "id": "2",
        "url": "unknown",
        "title": "Unknown",
        "body": "Even the greatest was once a beginner. Don't be afraid to take that first step.",
        "category": "bar",
        "tags": [ "foo", "bar", "baz" ]
      },
      {
        "id": "3",
        "url": "neale-donald-walsch",
        "title": "Neale Donald Walsch",
        "body": "You are afraid to die, and you're afraid to live. What a way to exist.",
        "category": "bar",
        "tags": [ "foo", "bar", "baz" ]
      }
    ]
  },
  {
    "users": [
      { "name": "Admin" },
      { "name": "User" }
    ]
  }
]
</code></pre>

<p>Which is called into my API router this way:</p>

<pre><code>function fsAsync(callback) {
  fs.readFile(__dirname + '/../public/articles/data.json', 'utf8', function(err, data) {
    if (err) {
      return callback(err);
    }
    callback(null, JSON.parse(data));
  });
};
</code></pre>

<p>I calling every articles like this at this route: <code>http://127.0.0.1:3000/api/articles/&lt;API-TOKEN&gt;</code></p>

<pre><code>router.get('/articles/:token', function(req, res) {
  fsAsync(function(err, data) {
    if (err) {
      return res.send(err);
    }
    var articles = data[0].articles;

    var q = articles.filter(function (article) {
      // return article.id === req.params.id;
      return article &amp;&amp; apiToken === req.params.token;
    });
    res.json(q);
  });
});
</code></pre>

<p>However I want to separate this API's output into different pages when I rendering this API route: <code>http://127.0.0.1:3000/api/articles/0/&lt;API-TOKEN&gt;</code></p>

<p>I tried to implement the <code>array.slice</code> method described here: <a href="https://stackoverflow.com/a/8495740/1442219">https://stackoverflow.com/a/8495740/1442219</a></p>

<p>How can it be achieved?</p>

<p>Thank You!</p>

<p><strong>Update:</strong></p>

<p>One thing could cause problem if I spliting a JSON object into chunks, than it will need to parse everything from that object first, than decide where to split? What if user visiting the last page at 567? This means the code first have to query through millions of millions of line to return the wanted page? What if it's a database, not a JSON with dummy data? In sort, what are the best practices to return a specific page for a blog from JSON / Mongodb / etc. source?</p>

<p><strong>Update 2:</strong></p>

<p>This returns the first two article from the object:</p>

<pre><code>// http://127.0.0.1:3000/api/articles/c/&lt;API-TOKEN&gt;
router.get('/articles/c/:token', function(req, res) {
  fsAsync(function(err, data) {
    if (err) {
      return res.send(err);
    }
    var articles = data[0].articles;

    var count = 0;
    var countMultiply = count * 2;
    var a = countMultiply + 0;
    var b = countMultiply + 2;
    var c = articles.slice(a, b);
    console.log(c);

    var q = c.filter(function (article) {
      // return article.id === req.params.id;
      return article &amp;&amp; apiToken === req.params.token;
    });
    res.json(q); // (0*2+0=0, 0*2+2=2), (1*2+0=2, 1*2+2=4), (2*2+0=4, 2*2+2=6), (3*2+0=6, 3*2+2=8)
  });
});
</code></pre>

<p>How can I automate this able to use 0, 1, 2, 3 for page separators? So the first page at <code>http://127.0.0.1:3000/api/articles/0/&lt;API-TOKEN&gt;</code> URL to return articles <code>(0,2)</code> and the second page at <code>http://127.0.0.1:3000/api/articles/1/&lt;API-TOKEN&gt;</code> URL to return articles <code>(2,4)</code>?</p>