Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Update vhost to use a table for all vhosts

  • Loading branch information...
commit d37790a6fcb856e7a7cdf24750f82416ba5b0652 1 parent ba516e5
Tim Caswell authored December 29, 2011

Showing 1 changed file with 11 additions and 7 deletions. Show diff stats Hide diff stats

  1. 18  vhost.js
18  vhost.js
... ...
@@ -1,13 +1,17 @@
1 1
 
2  
-module.exports = function setup(domain, handler) {
  2
+module.exports = function setup(domainHandlers, defaultHandler) {
  3
+  var domains = Object.keys(domainHandlers);
  4
+  var length = domains.length;
3 5
   return function handle(req, res, next) {
4 6
     var host = req.headers.host;
5  
-    var i = host.indexOf(":");
6  
-    if (i >= 0) host = host.substr(0, i);
7  
-    if (host === domain) {
8  
-      handler(req, res, next);
9  
-    } else {
10  
-      next();
  7
+    var p = host.indexOf(":");
  8
+    if (p >= 0) host = host.substr(0, p);
  9
+    for (var i = 0; i < length; i++) {
  10
+      var domain = domains[i];
  11
+      if (domain !== host) continue;
  12
+      return domainHandlers[domain](req, res, next);
11 13
     }
  14
+    if (defaultHandler) return defaultHandler(req, res, next);
  15
+    next();
12 16
   };
13 17
 };

0 notes on commit d37790a

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