req.ip
The remote IP address of the request.
Syntax
req.ip / req.ips Returns
string — The client IP; req.ips is the X-Forwarded-For chain when trust proxy is set.
Examples
app.get('/whoami', (req, res) => {
res.send(req.ip);
});
Output
$ curl localhost:3000/whoami
::1
app.set('trust proxy', 1);
app.get('/ips', (req, res) => {
res.json({ ip: req.ip, chain: req.ips });
});
Output
{"ip":"203.0.113.5","chain":["203.0.113.5"]}
Notes
Behind a proxy or load balancer `req.ip` is the proxy address unless
you set `app.set('trust proxy', ...)` so Express reads
`X-Forwarded-For`. Only trust that header for proxies you control,
since clients can forge it.