Skip to content
This repository
Browse code

Add subdomain offset setting

Add a setting "subdomain offset" for the app, which can be used to
change the behavior of req.subdomains. This is useful when our "base"
domain contains more than two parts, e.g. example.co.uk, and also
when we are running locally with domains like xxx.local.

The default behavior is still to return all but the last two parts.
  • Loading branch information...
commit ba00e236302e9f74d0d35c1dd8522580076cab9b 1 parent 8beb1f2
Greg Methvin authored January 20, 2013
17  lib/request.js
@@ -401,18 +401,23 @@ req.__defineGetter__('auth', function(){
401 401
 /**
402 402
  * Return subdomains as an array.
403 403
  *
404  
- * For example "tobi.ferrets.example.com"
405  
- * would provide `["ferrets", "tobi"]`.
  404
+ * Subdomains are the dot-separated parts of the host before the main domain of
  405
+ * the app. By default, the domain of the app is assumed to be the last two
  406
+ * parts of the host. This can be changed by setting "subdomain offset".
  407
+ *
  408
+ * For example, if the domain is "tobi.ferrets.example.com":
  409
+ * If "subdomain offset" is not set, req.subdomains is `["ferrets", "tobi"]`.
  410
+ * If "subdomain offset" is 3, req.subdomains is `["tobi"]`.
406 411
  *
407 412
  * @return {Array}
408 413
  * @api public
409 414
  */
410 415
 
411 416
 req.__defineGetter__('subdomains', function(){
412  
-  return this.get('Host')
413  
-    .split('.')
414  
-    .slice(0, -2)
415  
-    .reverse();
  417
+  var offset = this.app.get('subdomain offset');
  418
+  if (offset == null) offset = 2;
  419
+
  420
+  return this.get('Host').split('.').reverse().slice(offset);
416 421
 });
417 422
 
418 423
 /**
50  test/req.subdomains.js
@@ -33,5 +33,55 @@ describe('req', function(){
33 33
         .expect('[]', done);
34 34
       })
35 35
     })
  36
+
  37
+    describe('when subdomain offset is set', function(){
  38
+      describe('when subdomain offset is zero', function(){
  39
+        it('should return an array with the whole domain', function(done){
  40
+          var app = express();
  41
+          app.set('subdomain offset', 0);
  42
+
  43
+          app.use(function(req, res){
  44
+            res.send(req.subdomains);
  45
+          });
  46
+
  47
+          request(app)
  48
+          .get('/')
  49
+          .set('Host', 'tobi.ferrets.sub.example.com')
  50
+          .expect('["com","example","sub","ferrets","tobi"]', done);
  51
+        })
  52
+      })
  53
+
  54
+      describe('when present', function(){
  55
+        it('should return an array', function(done){
  56
+          var app = express();
  57
+          app.set('subdomain offset', 3);
  58
+
  59
+          app.use(function(req, res){
  60
+            res.send(req.subdomains);
  61
+          });
  62
+
  63
+          request(app)
  64
+          .get('/')
  65
+          .set('Host', 'tobi.ferrets.sub.example.com')
  66
+          .expect('["ferrets","tobi"]', done);
  67
+        })
  68
+      })
  69
+
  70
+      describe('otherwise', function(){
  71
+        it('should return an empty array', function(done){
  72
+          var app = express();
  73
+          app.set('subdomain offset', 3);
  74
+
  75
+          app.use(function(req, res){
  76
+            res.send(req.subdomains);
  77
+          });
  78
+
  79
+          request(app)
  80
+          .get('/')
  81
+          .set('Host', 'sub.example.com')
  82
+          .expect('[]', done);
  83
+        })
  84
+      })
  85
+    })
36 86
   })
37 87
 })

0 notes on commit ba00e23

Please sign in to comment.
Something went wrong with that request. Please try again.