很多时候前端需要向后端请求一系列的博文,并且希望能够设定分页、作者、标签等参数。
博文列表对象
我们先规定后端返回的博文列表的JSON对象应该满足如下格式:
{
"articlesCount": 2,
"articles":[{
"slug": "how-to-train-your-dragon",
"title": "How to train your dragon",
"tagList": ["dragons", "training"],
"description": "Ever wonder how?",
"createdAt": "2016-02-18T03:22:56.637Z",
"updatedAt": "2016-02-18T03:48:35.824Z",
"favorited": false,
"favoritesCount": 0,
"author": {
"username": "jake",
"bio": "I work at statefarm",
"image": "https://i.stack.imgur.com/xHWG8.jpg",
"following": false
}
}, {
"slug": "how-to-train-your-dragon-2",
"title": "How to train your dragon 2",
"tagList": ["dragons", "training"],
"description": "So toothless",
"createdAt": "2016-02-18T03:22:56.637Z",
"updatedAt": "2016-02-18T03:48:35.824Z",
"favorited": false,
"favoritesCount": 0,
"author": {
"username": "jake",
"bio": "I work at statefarm",
"image": "https://i.stack.imgur.com/xHWG8.jpg",
"following": false
}
}]
}
读取博文列表
端点为GET /api/articles
,验证信息可有可无,按时间倒序返回若干篇博文,接受下列query参数:
tag=React
author=jake
-
favorited=jake
,返回被指定用户点赞的博文 -
limit=20
,默认值为20 offset=20
读取时间线
端点为GET /api/articles/feed
,需要验证信息,按时间倒序返回当前用户所关注者发表的博文,也可以接受如上的limit
与tag
参数。
下一步
到此为止,我们把整个API大概剖解了一遍。下一篇文章开始正式进入本系列的第三部分:后端的开发。
网友评论