The `forwarded` package is a minimalist Node.js utility designed to robustly parse the `X-Forwarded-For` HTTP header. It extracts a list of IP addresses from the request, including the direct socket address, and returns them in reverse proxy order, where index `0` represents the immediate client (the socket address) and the last index is the furthest origin (typically the end-user's IP). This is crucial for applications running behind proxies or load balancers, where `req.ip` or `req.connection.remoteAddress` would only reflect the proxy's IP. The current stable version is 0.2.0, released in May 2021. Being part of the `jshttp` organization, `forwarded` adheres to a philosophy of creating small, single-purpose, and highly performant modules. Its release cadence is exceptionally slow, with only four releases since its initial launch in 2014, which underscores its stability and the mature nature of its functionality. It has no external runtime dependencies, making it a lightweight and reliable choice for foundational HTTP parsing tasks.
npm install forwardedVerified import paths — ran on the pinned version, not inferred.
This example demonstrates how to use the `forwarded` function with a mock HTTP request object, showing how it parses the `X-Forwarded-For` header and includes the socket's remote address in the correct reverse proxy order, even when the header is absent.
Ensure your Node.js environment is up-to-date and avoid relying on deprecated `req.connection` properties directly in your application code. The `forwarded` package transparently handles the correct property access.
Always remember that the array returned by `forwarded(req)` is in reverse order of proxies. If you need the original client's IP, it will typically be the last element of the array.
For CommonJS, use `const forwarded = require('forwarded')`. For ESM, use `import forwarded from 'forwarded'` to correctly import the default export.Ensure the argument passed to `forwarded()` is a valid Node.js `IncomingMessage` object or a compatible mock object that includes a `headers` property (even if empty) and a `socket.remoteAddress` property.
Verify that your proxy or load balancer is correctly forwarding the `X-Forwarded-For` header to your Node.js application. Also, ensure you are using the last element of the array returned by `forwarded(req)` to get the furthest client IP.
No dependency data recorded yet.