forked from bmullan91/express-subdomain
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
40 lines (32 loc) · 1.06 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
module.exports = function(subdomain, fn) {
if(!subdomain || typeof subdomain !== "string") {
throw new Error("The first parameter must be a string representing the subdomain");
}
//check fn handles three params..
if(!fn || typeof fn !== "function" || fn.length < 3) {
throw new Error("The second parameter must be a function that handles fn(req, res, next) params.");
}
return function (req, res, next) {
req._subdomainLevel = req._subdomainLevel || 0;
var subdomainSplit = subdomain.split('.');
var len = subdomainSplit.length;
var match = true;
//url - v2.api.example.dom
//subdomains == ['api', 'v2']
//subdomainSplit = ['v2', 'api']
for(var i = 0; i < len; i++) {
var expected = subdomainSplit[len - (i+1)];
var actual = req.subdomains[i+req._subdomainLevel];
if(expected === '*') { continue; }
if(actual !== expected) {
match = false;
break;
}
}
if(match) {
req._subdomainLevel++;//enables chaining
return fn(req, res, next);
}
next();
};
};