Skip to content

Instantly share code, notes, and snippets.

@rla
Created March 18, 2017 16:19
Show Gist options
  • Save rla/ce46909f4aa1cf742f63496c1884c0c1 to your computer and use it in GitHub Desktop.
Save rla/ce46909f4aa1cf742f63496c1884c0c1 to your computer and use it in GitHub Desktop.
Middleware to redirect to HTTPS.
// Middleware to redirect to HTTPS.
module.exports = () => {
return (req, res, next) => {
if (req.protocol !== 'https') {
const host = req.hostname;
if (host && !req.url.match(/\.well-known/)) {
res.redirect(301, `https://${host}${req.url}`);
} else {
next();
}
} else {
next();
}
};
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment