Express req.protocol Property

Beginner
⏱️ 8 min read
📚 Updated: May 2026
🎯 4 Code Examples

What you’ll learn

  • How req.protocol reports http vs https.
  • How reverse proxies and trust proxy affect protocol detection.
  • How to use req.secure for HTTPS-only logic.
  • How to build absolute URLs using request protocol data.

Usage syntax

javascript
req.protocol
req.secure
1

Redirect HTTP traffic to HTTPS

javascript
app.use(function (req, res, next) {
  if (!req.secure) {
    return res.redirect('https://' + req.get('host') + req.originalUrl);
  }
  next();
});
2

Build absolute URL from request

javascript
app.get('/self', function (req, res) {
  var url = req.protocol + '://' + req.get('host') + req.originalUrl;
  res.send(url);
});

❓ FAQ

It indicates the request protocol, typically http or https.
Express uses connection info and, with trust proxy enabled, forwarded protocol headers.
req.secure is a boolean shortcut equivalent to req.protocol === 'https'.
Usually trust proxy is not configured, so Express cannot trust forwarded protocol headers.
Yes, combine req.protocol with req.get('host') for full URL generation.
Did you know?

req.protocol returns http or https; behind proxies it depends on proper trust proxy configuration.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

4 people found this page helpful