express-subdomain-handler
Advanced tools
Comparing version 0.0.2 to 0.0.3
@@ -10,20 +10,37 @@ | ||
* | ||
* USAGE: | ||
* | ||
* // add express-subdomain-handler to your express middleware stack | ||
* app.use( require('express-subdomain-handler')({ baseUrl: 'example.com', prefix: 'myprefix', logger: true }) ); | ||
* | ||
* // setup routes to catch subdomain urls | ||
* // eg. 'http://mysubdomain.example.com/homepage' | ||
* app.get('/myprefix/:thesubdomain/thepage', function(req, res, next){ | ||
* | ||
* // for the example url this will print 'mysubdomain' | ||
* res.send(req.params.thesubdomain); | ||
* | ||
* }); | ||
*/ | ||
function _escapeRegExp(str) { | ||
return str.replace(/[-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g, "\\$&"); | ||
} | ||
/** | ||
* Escapes special character in a string for use in | ||
* a regex. | ||
* Creates the custom regex object from the specified baseUrl | ||
* | ||
* @param {string} str | ||
* @return {string} | ||
* @param {string} baseUrl [description] | ||
* @return {Object} the regex object | ||
*/ | ||
function escapeRegExp(str) { | ||
return str.replace(/[-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g, "\\$&"); | ||
} | ||
function subdomainRegex(baseUrl){ | ||
var regex; | ||
baseUrl = _escapeRegExp(baseUrl); | ||
regex = new RegExp('((?!www)\\b[\\w\\.]+)\\.' + baseUrl + '(?::)?(?:\d+)?'); | ||
function subdomainRegex(baseUrl){ | ||
return regex = new RegExp("((?!www)\\b[\\w\\.]+)\\." + escapeRegExp(baseUrl) + "(?::)?(?:\d+)?"); | ||
return regex; | ||
} | ||
@@ -35,9 +52,22 @@ | ||
// defaults | ||
// prefix default | ||
options.prefix = options.prefix || 'subdomain'; | ||
// logger default | ||
options.logger = (options.logger === true) ? true : false; | ||
// allow 'base' or 'baseUrl' | ||
options.baseUrl = options.baseUrl || options.base; | ||
// return error if no baseUrl was specified | ||
if(!options.baseUrl) return next("express-subdomain-handler: You haven't specified a baseUrl, this is required!"); | ||
if(options.logger) console.log("\nESH: Rewriting all subdomain routes to /%s/[subdomain1]/[subdomain2]/path/after/baseurl for baseUrl %s \n", options.prefix, options.baseUrl); | ||
// the returned function | ||
return function(req, res, next){ | ||
var i, | ||
var | ||
// original req.url | ||
originalUrl = req.headers.host + req.url, | ||
@@ -48,6 +78,4 @@ // create our subdomain regex | ||
// extract the subdomain string from the req.url | ||
subdomainString = regex.exec(req.headers.host)[1]; | ||
//console.log(req.headers.host); | ||
//console.log(regex); | ||
console.log(subdomainString); | ||
subdomainString = regex.exec(req.headers.host); | ||
// if there is no subdomain, return | ||
@@ -57,7 +85,10 @@ if(!subdomainString) return next(); | ||
// create an array of subdomains | ||
subdomainArray = subdomainString.split('.'); | ||
subdomainArray = subdomainString[1].split('.'); | ||
// prend each subdomain | ||
subdomainArray.forEach(function(subdomain){ req.url = '/' + subdomain + req.url; }); | ||
// cache number of subdomains | ||
i = subdomainArray.length; | ||
// prepend each subdomain in reverse order | ||
while(i--){ req.url = '/' + subdomainArray[i] + req.url; } | ||
// finally prepend the suffix so we end up with | ||
@@ -67,3 +98,4 @@ // something like: "/subdomain/[first]/[second]/page" | ||
console.log('req.url: ' + req.url); | ||
// if logging is turned on | ||
if(options.logger) console.log('ESH: %s => %s', originalUrl, req.url); | ||
@@ -70,0 +102,0 @@ // jump to next middleware in stack |
{ | ||
"name": "express-subdomain-handler", | ||
"version": "0.0.2", | ||
"version": "0.0.3", | ||
"author": "Wilson Page", | ||
@@ -5,0 +5,0 @@ "description": "A tiny module to handle subdomains nicely in Express", |
9488
9
144
38