Skip to content

Commit bb27d53

Browse files
committed
Fix redirect bug when the server runs on a port different from 443
Redirect server previously runned on custom port (default 80) but redirected always on 443. Now is possible to specify both the http and https ports for redirect.
1 parent 09984fe commit bb27d53

File tree

4 files changed

+247
-109
lines changed

4 files changed

+247
-109
lines changed

index.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,13 +37,15 @@ const createServer = () => {
3737

3838
// redirect http to https, usage `app.redirect()`
3939
app.redirect = function(
40-
/* istanbul ignore next: cannot be tested on Travis */ port = 80) {
40+
/* istanbul ignore next: cannot be tested on Travis */ httpPort = 80,
41+
httpsPort = process.env.PORT || 443) {
4142
app.http = http.createServer((req, res) => {
4243
res.writeHead(301, {
43-
Location: "https://" + req.headers["host"] + req.url
44+
Location: "https://" + req.headers["host"].replace(":" + httpPort, "") +
45+
(httpsPort !== 443 ? ":" + httpsPort : "") + req.url
4446
})
4547
res.end()
46-
}).listen(port)
48+
}).listen(httpPort)
4749
console.info("http to https redirection active.")
4850
}
4951

@@ -94,7 +96,7 @@ process.on("uncaughtException", function(err) {
9496
break
9597
case "EADDRINUSE":
9698
console.error("EADDRINUSE: another service on your machine is using " +
97-
"port 443 or port 80.\nStop it or change port with:" +
99+
"the current port.\nStop it or change port with:" +
98100
"`PORT=4433 serve ~/myproj`.")
99101
break
100102
default:

0 commit comments

Comments
 (0)