Skip to content

Commit

Permalink
fix redirect loop
Browse files Browse the repository at this point in the history
  • Loading branch information
geoffrey-wu committed Oct 12, 2024
1 parent bd039ef commit ada566a
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 1 deletion.
2 changes: 2 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ if (process.env.NODE_ENV !== 'production') {
app.use(morgan('dev'));
}

// https://stackoverflow.com/questions/10348906/how-to-know-if-a-request-is-http-or-https-in-node-js
app.enable('trust proxy');
app.use(hostnameRedirection);
app.use(httpsEnforcement);

Expand Down
2 changes: 1 addition & 1 deletion server/https-enforcement.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ export default function httpsEnforcement (req, res, next) {
const hostname = req.hostname;

// Use HTTPS if not on localhost
if (req.protocol !== 'https' && !['localhost', '127.0.0.1'].includes(hostname)) {
if (!req.secure && !['localhost', '127.0.0.1'].includes(hostname)) {
return res.redirect(301, `https://${hostname}${req.originalUrl}`);
}

Expand Down

0 comments on commit ada566a

Please sign in to comment.