我想将API的输出分成不同的页面。我想这样称呼他们:http://127.0.0.1:3000/api/articles/0/<API-TOKEN>
返回第一页的2-3篇等文章。
可在此处找到完整代码:https://github.com/DJviolin/horgalleryNode/blob/master/routes/api.js
我有虚拟数据JSON文件:
[
{
"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" }
]
}
]
以这种方式调用我的API路由器:
function fsAsync(callback) {
fs.readFile(__dirname + '/../public/articles/data.json', 'utf8', function(err, data) {
if (err) {
return callback(err);
}
callback(null, JSON.parse(data));
});
};
我在这条路线上打电话给每一篇文章:http://127.0.0.1:3000/api/articles/<API-TOKEN>
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 && apiToken === req.params.token;
});
res.json(q);
});
});
但是,当我渲染此API路线时,我想将此API的输出分隔到不同的页面:http://127.0.0.1:3000/api/articles/0/<API-TOKEN>
我尝试实施此处描述的array.slice
方法:https://stackoverflow.com/a/8495740/1442219
如何实现?
谢谢!
更新
如果我将JSON对象拆分成块,有一件事可能会导致问题,而不是先从该对象解析所有内容,而不是决定拆分的位置?如果用户访问567的最后一页怎么办?这意味着代码首先必须通过数百万行查询才能返回想要的页面?如果它是数据库,而不是具有虚拟数据的JSON,该怎么办?在排序中,从JSON / Mongodb /等来源返回博客特定页面的最佳做法是什么?
更新2:
这将从对象返回前两篇文章:
// http://127.0.0.1:3000/api/articles/c/<API-TOKEN>
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 && 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)
});
});
如何自动将此功能用于页面分隔符的0,1,2,3?因此,http://127.0.0.1:3000/api/articles/0/<API-TOKEN>
网址的第一页返回文章(0,2)
而第http://127.0.0.1:3000/api/articles/1/<API-TOKEN>
网址的第二页返回文章(2,4)
?
答案 0 :(得分:0)
更新3:
看起来它正在运作:
// http://127.0.0.1:3000/api/articles/page/0/<API-TOKEN>
router.get('/articles/page/:id/:token', function(req, res) {
fsAsync(function(err, data) {
if (err) {
return res.send(err);
}
var articles = data[0].articles.reverse();
var count = req.params.id; // Page number
var multiplier = 2; // Posts per page
var countResult = count * multiplier;
var a = countResult + 0;
var b = countResult + multiplier;
var c = articles.slice(a, b);
var pagesLength = articles.length / multiplier;
var pagesLengthCeil = Math.ceil(pagesLength); // Sum of all pages
console.log('pagesLengthCeil: ' + pagesLengthCeil);
console.log(c);
var q = c.filter(function (article) {
// return article.id === req.params.id;
return article && 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)
});
});
但是我仍然不知道用巨大的JSON文件或数据库来做这个是一种有效的方法吗?