connect-redirecthost
Advanced tools
Comparing version 0.0.2 to 0.0.3
@@ -21,2 +21,3 @@ /*! | ||
* which supports specifying domains which should not be redirected. | ||
* In such cases, the root relative path is preserved. | ||
* | ||
@@ -34,2 +35,21 @@ * ```redirectHost({ | ||
* | ||
* Alternatively, should the root relative path be reset, | ||
* the following options can be used, where the path will | ||
* be set to the associated domain's value, or function | ||
* return value | ||
* | ||
* ```redirectHost({ | ||
* to: 'www.example.com', | ||
* changePath: {'www.example.ca': '/ca', 'www.example.us': '/us'} | ||
* });``` | ||
* | ||
* ```redirectHost({ | ||
* to: 'www.example.com', | ||
* changePath: { | ||
* 'www.example.ca': function(host, url){return '/ca' + url;}, | ||
* 'www.example.us': function(host, url){return '/us' + url;} | ||
* } | ||
* });``` | ||
* | ||
* | ||
* @param {String || Object} options | ||
@@ -45,2 +65,19 @@ * @return {Function} middleware function(req, res, next) | ||
// Determine if there is anything that must change the paths | ||
var pathFunc = function(host, url){return url;}; | ||
if(options.changePath){ | ||
pathFunc = function(host, url){ | ||
var override = options.changePath[host]; | ||
if(override){ | ||
if(_.isFunction(override)){ | ||
return override(host, url); | ||
} | ||
return override; | ||
} | ||
return url; | ||
} | ||
} | ||
// Localhost is ignored for redirects | ||
@@ -54,3 +91,3 @@ var except = {}; | ||
except[options] = true; | ||
return createHandler(options, except); | ||
return createHandler(options, except, pathFunc); | ||
} | ||
@@ -75,3 +112,3 @@ | ||
return createHandler(to, except); | ||
return createHandler(to, except, pathFunc); | ||
} | ||
@@ -89,3 +126,3 @@ | ||
function createHandler(to, except){ | ||
function createHandler(to, except, pathFunc){ | ||
return function(req, res, next){ | ||
@@ -98,5 +135,5 @@ var host = (req.header('host') || '').split(':')[0]; // strip port from host | ||
}else{ | ||
res.redirect('http://' + to + url); | ||
res.redirect('http://' + to + pathFunc(host, url)); //<- change url based on host | ||
} | ||
}; | ||
} |
@@ -5,3 +5,3 @@ { | ||
"description": "Connect middleware for the Express.js framework that allows redirecting multiple domains to a default one", | ||
"version": "0.0.2", | ||
"version": "0.0.3", | ||
"repository": { | ||
@@ -8,0 +8,0 @@ "url": "git@github.com:perropicante/connect-redirecthost.git" |
@@ -39,3 +39,23 @@ # Connect Host Redirect | ||
### Path handling | ||
By default, the root-relative path is preserved during the redirect. Should the root relative path need to be modified reset, | ||
the following options can be used, where the path will be set to the associated domain's value | ||
```app.use(require('connect-redirecthost').redirectHost({ | ||
to: 'www.example.com', | ||
changePath: {'www.example.ca': '/ca', 'www.example.us': '/us'} | ||
}));``` | ||
You can also specify a function to determine the new path; | ||
```app.use(require('connect-redirecthost').redirectHost({ | ||
to: 'www.example.com', | ||
changePath: { | ||
'www.example.ca': function(host, url){return '/ca' + url;}, | ||
'www.example.us': function(host, url){return '/us' + url;} | ||
} | ||
}));``` | ||
## License | ||
@@ -42,0 +62,0 @@ |
@@ -17,3 +17,3 @@ /*! | ||
}; | ||
res.__defineGetter__("location", function(){ | ||
res.__defineGetter__('location', function(){ | ||
return this._location; | ||
@@ -62,19 +62,19 @@ }); | ||
assert.isTrue(calledNext, 'next() was not called'); | ||
assert.isTrue(calledNext, 'next() was not called for \'' + from + '\''); | ||
}; | ||
} | ||
vows.describe("Domain redirection").addBatch({ | ||
"Throws errors when improperly initialized" : { | ||
vows.describe('Domain redirection').addBatch({ | ||
'Throws errors when improperly initialized' : { | ||
topic : function(){return true}, | ||
"throws errors when options is undefined": function(ignore){ | ||
'throws errors when options is undefined': function(ignore){ | ||
assert.throws(function(){factory.redirectHost()}, ReferenceError); | ||
} | ||
}, | ||
"Redirect from many subdomains to single domain" : { | ||
'Redirect from many subdomains to single domain' : { | ||
topic : function(){ | ||
return factory.redirectHost("www.example.com"); | ||
return factory.redirectHost('www.example.com'); | ||
}, | ||
"middleware exists": function(middleware){ | ||
assert.equal("function", typeof middleware); | ||
'middleware exists': function(middleware){ | ||
assert.equal('function', typeof middleware); | ||
}, | ||
@@ -108,11 +108,11 @@ 'redirects from example.com to www.example.com': verifyRedirect( | ||
}, | ||
"Redirect from most subdomains to a single domain, except one" : { | ||
'Redirect from most subdomains to a single domain, except one' : { | ||
topic : function(){ | ||
return factory.redirectHost({ | ||
except:"cdn.example.com", | ||
to:"www.example.com" | ||
except:'cdn.example.com', | ||
to:'www.example.com' | ||
}); | ||
}, | ||
"middleware exists": function(middleware){ | ||
assert.equal("function", typeof middleware); | ||
'middleware exists': function(middleware){ | ||
assert.equal('function', typeof middleware); | ||
}, | ||
@@ -143,11 +143,11 @@ 'redirects from example.com to www.example.com': verifyRedirect( | ||
}, | ||
"Redirect from most subdomains to a single domain" : { | ||
'Redirect from most subdomains to a single domain' : { | ||
topic : function(){ | ||
return factory.redirectHost({ | ||
except:["cdn.example.com", "origin.example.com"], | ||
to:"www.example.com" | ||
except:['cdn.example.com', 'origin.example.com'], | ||
to:'www.example.com' | ||
}); | ||
}, | ||
"middleware exists": function(middleware){ | ||
assert.equal("function", typeof middleware); | ||
'middleware exists': function(middleware){ | ||
assert.equal('function', typeof middleware); | ||
}, | ||
@@ -176,3 +176,46 @@ 'redirects from example.com to www.example.com': verifyRedirect( | ||
'http://127.0.0.1:3000/') | ||
}, | ||
'Redirect from one domain to another with a specific path' : { | ||
topic : function(){ | ||
return factory.redirectHost({ | ||
changePath: { | ||
'www.example.co.uk': '/uk', | ||
'www.example.ca': function(host, url){return '/ca' + url;} | ||
}, | ||
except:['cdn.example.com', 'origin.example.com'], | ||
to:'www.example.com' | ||
}); | ||
}, | ||
'middleware exists': function(middleware){ | ||
assert.equal('function', typeof middleware); | ||
}, | ||
'redirects from example.com to www.example.com': verifyRedirect( | ||
'http://example.com/', | ||
'http://www.example.com/'), | ||
'redirects from anything.example.com to www.example.com': verifyRedirect( | ||
'http://anything.example.com/', | ||
'http://www.example.com/'), | ||
'redirect from www.example.co.uk to www.example.com/uk using static path': verifyRedirect( | ||
'http://www.example.co.uk/', | ||
'http://www.example.com/uk'), | ||
'redirect from www.example.ca/hello to www.example.com/ca/hello using path function': verifyRedirect( | ||
'http://www.example.ca/hello', | ||
'http://www.example.com/ca/hello'), | ||
'redirect avoided on www.example.com': verifyNext( | ||
'http://www.example.com'), | ||
'redirect avoided on www.example.com:80': verifyNext( | ||
'http://www.example.com:80'), | ||
'redirect avoided on cdn.example.com': verifyNext( | ||
'http://cdn.example.com'), | ||
'redirect avoided on origin.example.com': verifyNext( | ||
'http://origin.example.com'), | ||
'redirect skipped for localhost': verifyNext( | ||
'http://localhost/'), | ||
'redirect skipped for localhost:3000': verifyNext( | ||
'http://localhost:3000/'), | ||
'redirect skipped for 127.0.0.1': verifyNext( | ||
'http://127.0.0.1/'), | ||
'redirect skipped for 127.0.0.1:3000': verifyNext( | ||
'http://127.0.0.1:3000/') | ||
} | ||
}).export(module); |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
38450
15
318
65