Skip to content
This repository was archived by the owner on Jun 10, 2019. It is now read-only.

Commit b60354c

Browse files
alexspencerickr
authored andcommitted
forward http requests to https
1 parent 018f963 commit b60354c

1 file changed

Lines changed: 6 additions & 17 deletions

File tree

server.js

Lines changed: 6 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,27 +4,16 @@ const path = require('path');
44

55
const app = express();
66

7-
const FALLBACK_PORT = 4000;
8-
9-
// Redirect all HTTP traffic to HTTPS
10-
function ensureSecure(req, res, next) {
11-
if (req.headers['x-forwarded-proto'] === 'https') {
12-
return next();
13-
}
14-
let redirectUrl = `https://${req.hostname}`;
15-
let port = parseInt(process.env.PORT, 10);
16-
if (port !== 443) {
17-
if (!port) port = FALLBACK_PORT;
18-
redirectUrl = `${redirectUrl}:${port}`;
7+
app.use((req, res, next) => {
8+
if (!req.secure) {
9+
return res.redirect(`https://${req.headers.host}${req.url}`);
1910
}
20-
redirectUrl = redirectUrl.concat(req.url);
21-
return res.redirect(redirectUrl);
22-
}
23-
app.all('*', ensureSecure);
11+
return next();
12+
});
2413

2514
app.use(express.static('./build'));
2615
app.get('/*', (req, res) => {
2716
res.sendFile(path.join(__dirname, './build', 'index.html'));
2817
});
2918

30-
app.listen(process.env.PORT || FALLBACK_PORT);
19+
app.listen(process.env.PORT);

0 commit comments

Comments
 (0)