
Product
Introducing Tier 1 Reachability: Precision CVE Triage for Enterprise Teams
Socket’s new Tier 1 Reachability filters out up to 80% of irrelevant CVEs, so security teams can focus on the vulnerabilities that matter.
get-client-ip
Advanced tools
📍 A Lightweight Utility for Extracting the Real Client IP Address from Incoming HTTP Requests
A Lightweight Utility for Extracting
the Real Client IP Address from
Incoming HTTP Requests
get-client-ip
is a lightweight utility that extracts the real client IP address from an incoming HTTP request in Node.js.
It supports common proxy headers (x-forwarded-for
, x-real-ip
, etc.) and works seamlessly as both:
It adds req.clientIp
and req.clientIps
to the request object when used as middleware — no setup required.
npm install get-client-ip@latest
💡 Works with
npm
,pnpm
, andyarn
. You can use it in dev dependencies since it's typically used only for local HTTPS.
import http from 'node:http';
import express from 'express';
import { getClientIp } from 'get-client-ip';
import { env } from './env';
function bootstrap() {
const app = express();
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
// Standalone usage:
app.get('/standalone-ip', (req, res) => {
const ip = getClientIp(req);
res.status(200).json({ ip });
});
// Middleware usage:
app.get('/middleware-ip', getClientIp, (req, res) => {
res.status(200).json({ ip: req.clientIp, ips: req.clientIps });
});
http.createServer(app).listen(env.PORT || 3000, () => {
console.log(`🚀 Express server running on: http://localhost:${env.PORT || 3000}`);
});
}
bootstrap();
import { Controller, Get, Req } from '@nestjs/common';
import type { Request } from 'express';
import { getClientIp } from 'get-client-ip';
@Controller('')
export class PublicController {
@Get('ip')
getIp(@Req() req: Request) {
const ip = getClientIp(req);
return { ip };
}
}
The following headers are checked in order of precedence:
x-client-ip
x-forwarded-for
forwarded-for
x-forwarded
forwarded
x-real-ip
cf-connecting-ip
true-client-ip
x-cluster-client-ip
fastly-client-ip
x-appengine-user-ip
cf-pseudo-ipv4
It also falls back to:
req.ip;
req.socket.remoteAddress;
req.connection.remoteAddress;
We want to thank Petar Bojinov for the inspiration.
Want to contribute or suggest a feature?
This project is licensed under the MIT License.
Thank you!
FAQs
📍 A Lightweight Utility for Extracting the Real Client IP Address from Incoming HTTP Requests
The npm package get-client-ip receives a total of 425 weekly downloads. As such, get-client-ip popularity was classified as not popular.
We found that get-client-ip demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 2 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Product
Socket’s new Tier 1 Reachability filters out up to 80% of irrelevant CVEs, so security teams can focus on the vulnerabilities that matter.
Research
/Security News
Ongoing npm supply chain attack spreads to DuckDB: multiple packages compromised with the same wallet-drainer malware.
Security News
The MCP Steering Committee has launched the official MCP Registry in preview, a central hub for discovering and publishing MCP servers.