Express req.ip Property
What you’ll learn
- How Express determines
req.ip. - How to configure
trust proxycorrectly. - How
req.ipandreq.ipsdiffer. - How to use IP data safely for logging and rate limiting.
Usage syntax
javascript
req.ip
req.ips1
Read client IP in a route
javascript
app.get('/ip', function (req, res) {
res.json({ ip: req.ip });
});2
Use req.ips behind trusted proxy
javascript
app.set('trust proxy', true);
app.get('/audit', function (req, res) {
res.json({
clientIp: req.ip,
forwardedChain: req.ips
});
});❓ FAQ
It returns the client IP address for the current request.
If your app is behind a reverse proxy and trust proxy is not configured, req.ip may show proxy addresses.
When enabled correctly, Express can use X-Forwarded-For to determine the original client IP.
req.ip is the selected client IP, while req.ips is the chain of forwarded IPs when trust proxy is enabled.
Forwarded headers can be spoofed unless your proxy and trust proxy settings are configured carefully.
Did you know?
req.ip reflects the remote address, and when trust proxy is enabled it can use forwarded client IP headers.
4 people found this page helpful
