Express.js req.secure Property

Beginner
5 min read
Updated: Nov 2026
3 examples
Request

Definition

req.secure is a Boolean that is true when Express treats the request as HTTPS. Officially it is a short-hand for req.protocol === 'https'.

Direct TLS to Node sets it from the encrypted socket. Behind a reverse proxy (nginx, a load balancer, Heroku, and so on), enable trust proxy so Express can honor X-Forwarded-Proto. Related: req.protocol and app.set().

Syntax

Read req.secure in your Express.js route handler. Start with this line.

Express
req.secure
req.protocol === 'https'
CallWhat it does
req.securePrimary signature.
req.protocol === 'https'Related call form.

Install with npm install express. Create the app with const app = express() before you use this API in an Express.js tutorial project.

Library declarations
Express
const express = require("express");
const app = express();

req.secure
req.protocol === 'https'

Parameters

This Express.js property is read from the request or response — it has no call parameters.

Return Value

req.secure is a Boolean shorthand for req.protocol === 'https', respecting trust proxy.

Express
if (!req.secure && process.env.NODE_ENV === 'production') {
  return res.redirect('https://' + req.headers.host + req.originalUrl);
}

Express.js Examples

Example 1 — Read req.secure and req.protocol

Confirm the official equivalence on a diagnostics route.

Express
const express = require('express');
const app = express();

app.get('/diag', (req, res) => {
  res.json({
    secure: req.secure,
    protocol: req.protocol,
    sameAsHttps: req.secure === (req.protocol === 'https')
  });
});

app.listen(3000);

How It Works

  1. Getter runs. Express computes req.protocol, then req.secure as protocol === 'https'.
  2. Local HTTP. A normal http:// listen shows secure: false.
  3. Same check. sameAsHttps is always true — they are not independent flags.
  4. Use either form. Prefer req.secure for boolean branches; prefer req.protocol when you need the string.

Example 2 — Redirect HTTP to HTTPS

Common middleware pattern: send browsers to the secure URL.

Express
const express = require('express');
const app = express();

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

app.get('/', (req, res) => {
  res.send('Secure OK');
});

app.listen(3000);

How It Works

  1. Gate every request. Middleware runs first and checks !req.secure.
  2. Build the HTTPS URL. Host from req.get('host'), path/query from req.originalUrl.
  3. Redirect and stop. return after res.redirect so later routes do not run.
  4. Secure traffic continues. When req.secure is true, next() reaches the real handler.

Example 3 — Detect HTTPS Behind a Proxy

Terminate TLS at the proxy, then trust one hop so X-Forwarded-Proto is honored.

Express
const express = require('express');
const app = express();

// Trust the first proxy hop (adjust to your network)
app.set('trust proxy', 1);

app.get('/diag', (req, res) => {
  res.json({
    secure: req.secure,
    protocol: req.protocol,
    // Proxy often sends: X-Forwarded-Proto: https
  });
});

app.listen(3000);

How It Works

  1. TLS ends at the proxy. Express may see a plain HTTP socket from the proxy.
  2. Enable trust. app.set('trust proxy', 1) tells Express to trust one proxy hop.
  3. Read forwarded proto. With trust on, X-Forwarded-Proto: https makes req.protocol 'https'.
  4. req.secure follows. Because it is protocol === 'https', it becomes true.

Runtime Support

req.secure is an Express.js request property. It runs on a Node.js server (Express 5 needs Node.js 18+). Browsers do not have this API.

✓ Server · Express 4 & 5

req.secure

Works wherever Express runs: Node.js is the primary target. Bun and Deno can run many Express apps with compatibility layers. Not available in the browser.

Server Node.js runtime API
Node.js 18+ for Express 5 · 14+ typical for Express 4
Supported
Bun Express-compatible server apps
Supported
Deno Via npm compatibility / adapters
Supported
req.secure Express on Node.js

Bottom line: Study and run these examples with Node.js and the express package. Copy a snippet into a local file and start the server with node.

Frequently Asked Questions

It is a Boolean that is true when the request is treated as HTTPS. Officially it is equivalent to req.protocol === 'https'.
The TLS connection often terminates at the proxy, so Express sees plain HTTP unless you enable trust proxy and the proxy sends X-Forwarded-Proto: https.
Only when HTTPS is terminated upstream. Direct TLS to Node already makes req.secure true without trust proxy.
Only if your last trusted proxy overwrites X-Forwarded-For, X-Forwarded-Host, and X-Forwarded-Proto. Prefer a hop count or explicit proxy IPs when you can.
No. It is a server-side Express property. Browsers only send HTTP/HTTPS requests to your app.

Next: req.signedCookies

Read signed cookies that cookie-parser has verified.

req.signedCookies tutorial →

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.

8 people found this page helpful