-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
52 lines (42 loc) · 1 KB
/
index.js
File metadata and controls
52 lines (42 loc) · 1 KB
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
44
45
46
47
48
49
50
51
52
/**
* @param {String} forceHost
* @returns {Function}
*/
module.exports = function createRebindHost (forceHost) {
/**
* @param {Object} req
* @param {Object} res
* @param {Function} next
*/
return function rebindHost (req, res, next) {
var host = determineHost(req, forceHost);
if (!host) {
return next();
}
var getter = function getter () {
return host;
};
Object.defineProperty(req, 'host', {
configurable: true,
enumerable: true,
get: getter
});
next();
};
};
/**
* @param {Object} req
* @param {String} [forceHost]
* @returns {String}
*/
function determineHost (req, forceHost) {
var trust = req.app.get('trust proxy fn');
var host = req.get('X-Forwarded-Host');
if (!host || !trust(req.connection.remoteAddress, 0)) {
host = req.get('Host');
}
if (!!forceHost) {
host = forceHost;
}
return host;
}