Skip to content

Instantly share code, notes, and snippets.

@asherccohen
Created December 17, 2019 08:44
Show Gist options
  • Save asherccohen/d3bc442bbf460423bda4f4f8ea9d01a4 to your computer and use it in GitHub Desktop.
Save asherccohen/d3bc442bbf460423bda4f4f8ea9d01a4 to your computer and use it in GitHub Desktop.
secure-middleware
const secureRedirectMiddleware = ({ port, dev }) => (req, res, next) => {
if (!dev && !req.secure) {
const protocol = dev ? 'http' : 'https';
const portNumber = dev ? `:${port}` : '';
const url =`${protocol}://${req.hostname}${portNumber}${req.originalUrl}`;
res.redirect(301, url);
}
else {
next();
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment