最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to access 'rel' from Links in header? Hypermedia link relations - Stack Overflow

programmeradmin2浏览0评论

I am using json server and axios

result from header

link: "<http://localhost:3001/posts?_page=1>; rel="first", <http://localhost:3001/posts?_page=2>; rel="next", <http://localhost:3001/posts?_page=5>; rel="last""

How can I use/access these data from link? There seems to be no information about how to parse or access this aside from github. I tried link.rels[:last] like from github but it doesnt work.

I am using json server and axios

result from header

link: "<http://localhost:3001/posts?_page=1>; rel="first", <http://localhost:3001/posts?_page=2>; rel="next", <http://localhost:3001/posts?_page=5>; rel="last""

How can I use/access these data from link? There seems to be no information about how to parse or access this aside from github. I tried link.rels[:last] like from github but it doesnt work.

Share Improve this question edited Nov 23, 2017 at 7:27 Þaw asked Nov 23, 2017 at 6:39 ÞawÞaw 2,0574 gold badges22 silver badges40 bronze badges 4
  • 2 Did you check this link: stackoverflow./questions/8735792/…? – 31piy Commented Apr 15, 2018 at 3:48
  • I had not seen that one. very helpful – Catfish Commented Apr 15, 2018 at 17:51
  • @Catfish, any update on this? – Tarun Lalwani Commented Apr 20, 2018 at 8:51
  • @TarunLalwani The link from 31piy is very helpful. It has a library to simply do this. I also decided to use the octokit js lib which has methods for handling this github./octokit/rest.js – Catfish Commented Apr 20, 2018 at 19:56
Add a ment  | 

2 Answers 2

Reset to default 7 +25

Since JS is quite flexible you can simply use

data = 'link: "<http://localhost:3001/posts?_page=1>; rel="first", <http://localhost:3001/posts?_page=2>; rel="next", <http://localhost:3001/posts?_page=5>; rel="last""'

function parseData(data) {
    let arrData = data.split("link:")
    data = arrData.length == 2? arrData[1]: data;
    let parsed_data = {}

    arrData = data.split(",")

    for (d of arrData){
        linkInfo = /<([^>]+)>;\s+rel="([^"]+)"/ig.exec(d)

        parsed_data[linkInfo[2]]=linkInfo[1]
    }

    return parsed_data;
}

console.log(parseData(data))

The output is

{ first: 'http://localhost:3001/posts?_page=1',
  next: 'http://localhost:3001/posts?_page=2',
  last: 'http://localhost:3001/posts?_page=5' }

var data = 'link: "<http://localhost:3001/posts?_page=1>; rel="first", <http://localhost:3001/posts?_page=2>; rel="next", <http://localhost:3001/posts?_page=5>; rel="last""'

var linkRegex = /\<([^>]+)/g;
var relRegex = /rel="([^"]+)/g;
var linkArray = [];
var relArray = [];
var finalResult = {};
var temp;
while ((temp = linkRegex.exec(data)) != null) {
    linkArray.push(temp[1]);
}
while ((temp = relRegex.exec(data)) != null) {
    relArray.push(temp[1]);
}

finalResult = relArray.reduce((object, value, index) => {
    object[value] = linkArray[index];
    return object;
}, {});

console.log(finalResult);

发布评论

评论列表(0)

  1. 暂无评论