req.protocol
The request protocol string, http or https, plus req.secure.
Syntax
req.protocol / req.secure Returns
string — 'http' or 'https'; req.secure is the boolean equivalent.
Examples
app.get('/proto', (req, res) => {
res.json({ protocol: req.protocol, secure: req.secure });
});
Output
{"protocol":"http","secure":false}
app.use((req, res, next) => {
if (!req.secure) {
return res.redirect(`https://${req.hostname}${req.originalUrl}`);
}
next();
});
Notes
Behind a TLS-terminating proxy you must set
`app.set('trust proxy', ...)` so Express honors `X-Forwarded-Proto`;
otherwise `req.protocol` is always `'http'` and HTTPS redirects
loop.