Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 36 additions & 7 deletions lib/response.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,17 +70,46 @@ res.status = function status(code) {
* last: 'http://api.example.com/users?page=5'
* });
*
* @param {Object} links
* or
*
* res.links([
* {
* href: 'http://api.example.com/users?page=2',
* rel: 'next',
* title: 'next chapter',
* type: 'text/plain;charset=UTF-8'
* },
* {
* href: 'http://api.example.com/users?page=5',
* rel: 'last',
* title: 'the grand finale',
* type: 'video/webm'
* }
* ]);
* @param {Object|Array} links
* @return {ServerResponse}
* @public
*/

res.links = function(links){
var link = this.get('Link') || '';
if (link) link += ', ';
return this.set('Link', link + Object.keys(links).map(function(rel){
return '<' + links[rel] + '>; rel="' + rel + '"';
}).join(', '));
res.links = function (links) {
var link = this.get('Link') || '';
if (link) link += ', ';
if (Array.isArray(links)) {
link += Object.keys(links).map(function (i) {
return Object.keys(links[i]).map(function (item) {
if (item === 'href')
return '<' + links[i][item] + '>';
else
return item + '="' + links[i][item] + '"';
}).join('; ')
}).join(', ')
}
else {
link += Object.keys(links).map(function (rel) {
return '<' + links[rel] + '>; rel="' + rel + '"';
}).join(', ')
}
return this.set('Link', link);
};

/**
Expand Down
28 changes: 28 additions & 0 deletions test/res.links.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,5 +42,33 @@ describe('res', function(){
.expect('Link', '<http://api.example.com/users?page=2>; rel="next", <http://api.example.com/users?page=5>; rel="last", <http://api.example.com/users?page=1>; rel="prev"')
.expect(200, done);
})
it('should set Link header field with an Array', function (done) {
var app = express();

app.use(function (req, res) {
res.links([
{
href: 'http://api.example.com/users?page=2',
rel: 'next',
title: 'next chapter',
type: 'text/plain;charset=UTF-8'
},
{
href: 'http://api.example.com/users?page=5',
rel: 'last',
title: 'the grand finale',
type: 'video/webm'
}
]);

res.end();
});

request(app)
.get('/')
.expect('Link', '<http://api.example.com/users?page=2>; rel="next"; title="next chapter"; type="text/plain;charset=UTF-8",'
+ ' <http://api.example.com/users?page=5>; rel="last"; title="the grand finale"; type="video/webm"')
.expect(200, done);
})
})
})