Skip to content

Commit bec30b2

Browse files
update getRoot function to handle port properly
1 parent 10e00b6 commit bec30b2

1 file changed

Lines changed: 13 additions & 2 deletions

File tree

service/src/express.js

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,19 @@ const express = require("express")
1414

1515
const app = express();
1616
app.use(function(req, res, next) {
17-
req.getRoot = function() {
18-
return req.protocol + "://" + req.get('host');
17+
req.getRoot = function () {
18+
const port = Number(req.headers['x-forwarded-port'] || req.socket.localPort);
19+
const protocol = req.protocol;
20+
const hostname = req.hostname;
21+
let includePort = false;
22+
23+
// Only include port if non-standard for protocol
24+
if ((protocol === 'http' && port !== 80) ||
25+
(protocol === 'https' && port !== 443)) {
26+
includePort = true;
27+
}
28+
29+
return protocol + '://' + hostname + (includePort ? (':' + port) : '');
1930
};
2031

2132
req.getPath = function() {

0 commit comments

Comments
 (0)