-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
43 lines (32 loc) · 1.21 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
41
42
43
var defaults = require('defaults');
// Force a specific domain. This is useful when you have both the .com & .org
// It can also be used to force "www.", but there's a better module for this
module.exports = function(options){
options = defaults(options, {
host: false, // The host to force
localhost: true, // Allow for localhost
permanent: true, // Make a permanent redirect
clean: false // If it's included from clean-url
});
return function(req, res, next){
// Better naming for variables
var current = req.get('Host');
// If we don't want to match anything we cannot force it to match something
if (!options.host) {
return next();
}
// localhost:[0-9]+ is exempted
if (options.localhost && current.match(/^localhost\:[0-9]+/)) {
return next();
}
// If the domain is the specified one, no problem
if(current.indexOf(options.host, current.length - options.host.length) > -1){
return next();
}
// Last case is the failing one
var newdomain = req.protocol + "://" + options.host + req.url;
if (options.clean)
return newdomain;
return res.redirect(options.permanent ? 301 : 307, newdomain);
};
};