Skip to content

extractClientIp

reference
1 min readUpdated

Kind: Function

Source: atloria-monorepo/apps/api/src/analytics/utils/tracking-utils.ts

Extract the client IP from a NestJS/Express request. Checks headers set by reverse proxies and CDNs in this order:

  1. CF-Connecting-IP (Cloudflare)
  2. X-Real-IP (nginx default)
  3. X-Forwarded-For (standard proxy header — first IP in the chain)
  4. req.ip / connection.remoteAddress (direct connection fallback)

Returns null if nothing usable is found (should be rare in production).

Signature

ts
function extractClientIp(req: {
  headers?: Record<string, string | string[] | undefined>;
  ip?: string;
  connection?: { remoteAddress?: string };
  socket?: { remoteAddress?: string };
}): string | null

Parameters

NameType
req`{
headers?: Record<string, stringstring[]
ip?: string;
connection?: { remoteAddress?: string };
socket?: { remoteAddress?: string };
}`

Returns: string | null

Was this page helpful?

Download as PDF