我想将我的API输出分隔为不同的页面。我想这样称呼他们:http://127.0.0.1:3000/api/articles/0/<API-TOKEN>
将JSON API的对象拆分为快速应用程序中的区块
哪个返回第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/etc.源文件返回博客的特定页面的最佳做法是什么?
更新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)
?
如果不遍历所有文章,就无法做到这一点。要查找特定的文章,您必须遍历所有数据。收集完所有数据后,您可以决定要发送的内容,因为_z限制** _和_ ** skip ** _可用于收集的数据。 – xdeepakv
我明白了。这只是与JSON数据或数据库的方式?因此,在每次请求时,服务器都必须通过数千行来返回访问者想要查看的特定5或10行数据。 – Lanti
您可以像优化它一样,在遍历和匹配时对文档进行计数。一旦文件尺寸达到_ **跳过+限制** _停止。这是你可以做的。 – xdeepakv