github API在http链接报头中发送json结果的分页数据:
Link: <https://api.github.com/repos?page=3&per_page=100>; rel="next",
<https://api.github.com/repos?page=50&per_page=100>; rel="last"
由于github API并不是使用此方法的唯一API (我想),我想问一下是否有人有一个有用的小片段来解析链接头(并将其转换为数组),以便我可以在我的js应用程序中使用它。
我用谷歌搜索了一下,但没有找到任何关于如何从json APIs解析分页的有用信息。
发布于 2015-05-01 01:36:09
The parse-link-header NPM module就是为了这个目的而存在的;its source can be found on github是在麻省理工学院的许可下(免费用于商业用途)。
安装非常简单,如下所示:
npm install parse-link-header
用法如下所示:
var parse = require('parse-link-header');
var parsed = parse('<https://api.github.com/repos?page=3&per_page=100>; rel="next", <https://api.github.com/repos?page=50&per_page=100>; rel="last"')
...after有parsed.next
,parsed.last
等的哪一个:
{ next:
{ page: '3',
per_page: '100',
rel: 'next',
url: 'https://api.github.com/repos?page=3&per_page=100' },
last:
{ page: '50',
per_page: '100',
rel: 'last',
url: ' https://api.github.com/repos?page=50&per_page=100' } }
发布于 2012-01-07 12:12:44
GitHub Java API中有一个PageLinks类,用于显示如何解析Link
报头。
发布于 2013-05-26 22:05:52
我在github上找到了wombleton/link-headers。它似乎是为浏览器制作的,而不是一个npm模块,但似乎修改它以在服务器端环境中工作并不难。它使用pegjs生成真正的RFC5988解析器,而不是字符串拆分,因此它应该适用于任何链接头,而不仅仅是Github的。
https://stackoverflow.com/questions/8735792
复制相似问题