Skip to content

Instantly share code, notes, and snippets.

@olegpolyakov
Created February 1, 2019 10:22
Show Gist options
  • Save olegpolyakov/ef842858538b5cdf40db21a352252a0d to your computer and use it in GitHub Desktop.
Save olegpolyakov/ef842858538b5cdf40db21a352252a0d to your computer and use it in GitHub Desktop.
Express middleware to ensure HTTPS
function https(req, res, next) {
if (req.secure)
next();
else if (req.method === 'GET')
res.redirect(301, `https://${req.headers.host}${req.originalUrl}`);
else
res.status(403).send('Для доступа к сайту необходимо использовать защищенное соединение через HTTPS.');
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment