Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Added `res.links(obj)`

  • Loading branch information...
commit e6129d8ba513d3473f098b28bc815d08d72a59fb 1 parent d2baf11
TJ Holowaychuk tj authored
Showing with 40 additions and 0 deletions.
  1. +21 −0 lib/response.js
  2. +19 −0 test/res.links.js
21 lib/response.js
View
@@ -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:
19 test/res.links.js
View
@@ -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"');
+ })
+ })
+})
Please sign in to comment.
Something went wrong with that request. Please try again.