Skip to content

Commit

Permalink
Added res.links(obj)
Browse files Browse the repository at this point in the history
  • Loading branch information
tj committed Jun 17, 2012
1 parent d2baf11 commit e6129d8
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 0 deletions.
21 changes: 21 additions & 0 deletions lib/response.js
Expand Up @@ -40,6 +40,27 @@ res.status = function(code){
return this;
};

/**
* Set Link header field with the given `links`.
*
* Examples:
*
* res.links({
* next: 'http://api.example.com/users?page=2',
* last: 'http://api.example.com/users?page=5'
* });
*
* @param {Object} links
* @return {ServerResponse}
* @api public
*/

res.links = function(links){
return this.set('Link', Object.keys(links).map(function(rel){
return '<' + links[rel] + '>; rel="' + rel + '"';
}).join(', '));
};

/**
* Send a response.
*
Expand Down
19 changes: 19 additions & 0 deletions test/res.links.js
@@ -0,0 +1,19 @@

var express = require('../')
, res = express.response;

describe('res', function(){
describe('.links(obj)', function(){
it('should set Link header field', function(){
res.links({
next: 'http://api.example.com/users?page=2',
last: 'http://api.example.com/users?page=5'
});

res.get('link')
.should.equal(
'<http://api.example.com/users?page=2>; rel="next", '
+ '<http://api.example.com/users?page=5>; rel="last"');
})
})
})

0 comments on commit e6129d8

Please sign in to comment.