Permalink
Browse files

Added `res.links(obj)`

  • Loading branch information...
1 parent d2baf11 commit e6129d8ba513d3473f098b28bc815d08d72a59fb @tj tj committed Jun 17, 2012
Showing with 40 additions and 0 deletions.
  1. +21 −0 lib/response.js
  2. +19 −0 test/res.links.js
View
21 lib/response.js
@@ -41,6 +41,27 @@ res.status = function(code){
};
/**
+ * 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.
*
* Examples:
View
19 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.