Express.js req.protocol Property

Beginner
5 min read
Updated: Nov 2026
3 examples
Request

Definition

In Express.js, req.protocol is a string with the request protocol: http, or https for TLS. Example: console.dir(req.protocol) // => 'http'.

When trust proxy is enabled, Express may use the X-Forwarded-Proto header from your reverse proxy. Configure trust with app.set('trust proxy', …). For a boolean check, use req.secure (protocol === 'https'). See also the Express behind proxies guide pattern.

Syntax

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

Express
req.protocol
CallWhat it does
req.protocolPrimary signature.

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.protocol

Parameters

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

Return Value

req.protocol is a string property, typically 'http' or 'https', based on the connection and trust proxy / forwarded proto headers.

Express
const url = req.protocol + '://' + req.get('host') + req.originalUrl;

Express.js Examples

Example 1 — Read http or https

Log and return the protocol string from the official API shape.

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

app.get('/proto', (req, res) => {
  console.dir(req.protocol);
  // => 'http'
  res.json({
    protocol: req.protocol,
    secure: req.secure
  });
});

app.listen(3000);

How It Works

  1. Client hits the route. Node accepts the connection.
  2. Express sets the string. Plain local HTTP → 'http'.
  3. req.secure mirrors it. false when protocol is not https.
  4. JSON shows both. Handy while learning the pair.

Example 2 — Protocol Behind a Reverse Proxy

Trust one hop so X-Forwarded-Proto can report client-facing HTTPS.

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

// TLS ends at the load balancer; app sees HTTP + this header
app.set('trust proxy', 1);

app.get('/proto', (req, res) => {
  // Header: X-Forwarded-Proto: https
  res.json({ protocol: req.protocol });
});

app.listen(3000);

How It Works

  1. Match the hop count. trust proxy: 1 fits a single reverse proxy.
  2. Proxy adds the header. X-Forwarded-Proto: https describes the client scheme.
  3. Express prefers the header. With trust on, req.protocol becomes 'https'.
  4. Without trust, the header is ignored. You keep seeing the socket protocol to Node.

Example 3 — Build an Absolute URL

Combine protocol, host, and original URL for redirects or callbacks.

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

app.set('trust proxy', 1);

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

app.listen(3000);

How It Works

  1. Scheme from protocol. Respects trusted forwarded proto.
  2. Host from the header. req.get('host') reads Host (or forwarded host when trusted).
  3. Path from originalUrl. Keeps path + query via req.originalUrl.
  4. Concatenate. One absolute URL string for links or OAuth callbacks.

Runtime Support

req.protocol is an Express.js request property. It runs on a Node.js server (Express 5 needs Node.js 18+). Browsers do not execute it — they only open http/https connections to your app.

✓ Server · Express 4 & 5

req.protocol

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.protocol 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 string with the request protocol: usually 'http' or 'https' for TLS.
When trust proxy is enabled, Express can use the X-Forwarded-Proto header from your reverse proxy. When trust is off, it uses the actual connection protocol.
req.protocol is the string ('http' or 'https'). req.secure is a boolean that is true when protocol is 'https'.
The TLS connection often terminates at the proxy. Without a matching trust proxy setting, Express sees the plain HTTP hop to your app.
No. It is a server-side Express request property. Browsers only send HTTP(S) requests; your app reads req.protocol on the server.

Next: req.query

Read key/value pairs from the URL query string.

req.query tutorial →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

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