Express.js req.ip Property

Beginner
5 min read
Updated: Nov 2026
3 examples
Request

Definition

In Express.js, req.ip is the remote IP address of the request. By default it comes from the connection socket (for example 127.0.0.1 on localhost).

When trust proxy is enabled, Express derives req.ip from the left-most entry in X-Forwarded-For. That header can be set by a proxy or by a client — configure trust carefully. This is a server-side property on the request object.

Syntax

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

Express
req.ip
CallWhat it does
req.ipPrimary 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.ip

Parameters

ParameterDescription
Direct connectionOptional. req.ip is the socket remote address
trust proxyOptional. Switches source to left-most X-Forwarded-For
Hardened proxyOptional. Prevents clients from inventing the client IP

Return Value

req.ip is a string property with the remote address Express believes is the client, respecting trust proxy.

Express
app.set('trust proxy', 1);
app.get('/', (req, res) => {
  res.send({ ip: req.ip });
});

Express.js Examples

Example 1 — Read the Remote IP

Return req.ip as JSON — same idea as the official docs console example.

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

app.get('/whoami', (req, res) => {
  console.log(req.ip); // e.g. '127.0.0.1'
  res.json({ ip: req.ip });
});

app.listen(3000);

How It Works

  1. Default trust is off. Express uses the TCP peer address.
  2. Local requests look local. Hitting localhost often yields 127.0.0.1 or ::1.
  3. No header parsing yet. X-Forwarded-For is ignored until you enable trust proxy.
  4. Expose for learning. Production apps usually log the IP instead of returning it publicly.

Example 2 — Log IP on Every Request

Lightweight middleware that records the client IP before routes run.

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

app.use((req, res, next) => {
  console.log(req.method, req.path, 'from', req.ip);
  next();
});

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

app.listen(3000);

How It Works

  1. Mount middleware early. It runs for every request before route handlers.
  2. Read req.ip once. Same value later routes would see (unless something mutates trust settings).
  3. Call next(). Logging must not swallow the request.
  4. Scale carefully. Real rate limiters use stores and careful proxy trust — this is only a teaching log.

Example 3 — trust proxy and X-Forwarded-For

Behind a reverse proxy, trust that hop so req.ip becomes the left-most forwarded client address.

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

// Trust one hop (your reverse proxy). Prefer IP/subnet in production.
app.set('trust proxy', 1);

app.get('/client-ip', (req, res) => {
  // X-Forwarded-For: 203.0.113.10, 10.0.0.5
  // → req.ip === '203.0.113.10'
  // → req.ips === ['203.0.113.10', '10.0.0.5']
  res.json({
    ip: req.ip,
    ips: req.ips
  });
});

app.listen(3000);

How It Works

  1. Proxy sits in front. The socket address is often the proxy, not the browser.
  2. Enable trust for that hop. Express then reads X-Forwarded-For.
  3. Left-most is the client. Official rule: first address in the list becomes req.ip.
  4. Use req.ips for the chain. Helpful when debugging multiple proxies.

Runtime Support

req.ip is an Express.js request property. It runs on a Node.js server (Express 5 needs Node.js 18+). Browsers do not expose this value to page scripts.

✓ Server · Express 4 & 5

req.ip

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.ip Express on Node.js

Bottom line: Configure trust proxy only when you are behind a real proxy, then read req.ip for the client address.

Frequently Asked Questions

It is the remote IP address for the request. By default it comes from the socket. With trust proxy enabled, Express uses the left-most address in X-Forwarded-For.
Your app is behind a reverse proxy and trust proxy is still false (the default). Configure trust proxy to match that proxy so forwarded headers are used.
req.ip is one selected client IP. req.ips is the full X-Forwarded-For chain as an array when trust proxy is on; otherwise it is [].
Yes if you trust X-Forwarded-For from untrusted clients. Only enable trust proxy for proxies you control, and have them overwrite spoofed forwarded headers.
The header is client, proxy1, proxy2. Express takes the left-most entry as req.ip when trust proxy is enabled — that is usually the original client.

Next: req.ips

See the full list of addresses from X-Forwarded-For when trust proxy is on.

req.ips 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