What is request-ip?
The request-ip npm package is used to retrieve a user's IP address from a request object in a Node.js application. It supports various types of requests and can extract the IP address from different sources such as headers, connection properties, and proxies.
What are request-ip's main functionalities?
Extract IP Address from Request
This feature allows you to extract the client's IP address from the request object in an Express.js application. The IP address is retrieved using the `getClientIp` method and can be used for logging, analytics, or other purposes.
const requestIp = require('request-ip');
const express = require('express');
const app = express();
app.use((req, res, next) => {
const clientIp = requestIp.getClientIp(req);
console.log(clientIp);
next();
});
app.get('/', (req, res) => {
res.send('Your IP address is: ' + requestIp.getClientIp(req));
});
app.listen(3000, () => {
console.log('Server is running on port 3000');
});
Support for Various Request Types
This feature demonstrates the ability to use the request-ip package with a plain Node.js HTTP server. The `getClientIp` method works seamlessly with different types of request objects, making it versatile for various Node.js applications.
const requestIp = require('request-ip');
const http = require('http');
const server = http.createServer((req, res) => {
const clientIp = requestIp.getClientIp(req);
res.writeHead(200, { 'Content-Type': 'text/plain' });
res.end('Your IP address is: ' + clientIp);
});
server.listen(3000, () => {
console.log('Server is running on port 3000');
});
Extract IP Address from Proxies
This feature shows how to configure an Express.js application to trust proxy headers and extract the client's IP address when the application is behind a proxy. The `trust proxy` setting is enabled, allowing the `getClientIp` method to correctly identify the client's IP address.
const requestIp = require('request-ip');
const express = require('express');
const app = express();
app.set('trust proxy', true);
app.use((req, res, next) => {
const clientIp = requestIp.getClientIp(req);
console.log(clientIp);
next();
});
app.get('/', (req, res) => {
res.send('Your IP address is: ' + requestIp.getClientIp(req));
});
app.listen(3000, () => {
console.log('Server is running on port 3000');
});
Other packages similar to request-ip
ip
The 'ip' package provides utilities for IP address manipulation, including functions to validate, convert, and compare IP addresses. While it offers more general IP address utilities, it does not specifically focus on extracting IP addresses from request objects like request-ip.
forwarded
The 'forwarded' package is used to parse the X-Forwarded-For header to get the client's IP address. It is more specialized in handling forwarded headers but lacks the broader support for different request types and sources that request-ip provides.
express-ip
The 'express-ip' package is an Express.js middleware for retrieving the client's IP address. It is similar to request-ip but is specifically designed for Express.js applications and may not be as versatile for other types of Node.js applications.
request-ip
A tiny Node.js module for retrieving a request's IP address.
Installation
npm install request-ip --save
Getting Started
const requestIp = require('request-ip');
const ipMiddleware = function(req, res, next) {
const clientIp = requestIp.getClientIp(req);
next();
};
As Connect Middleware
const requestIp = require('request-ip');
app.use(requestIp.mw())
app.use(function(req, res) {
const ip = req.clientIp;
res.end(ip);
});
To see a full working code for the middleware, check out the examples folder.
The connect-middleware also supports retrieving the ip address under a custom attribute name, which also works as a container for any future settings.
How It Works
It looks for specific headers in the request and falls back to some defaults if they do not exist.
The following is the order we use to determine the user ip from the request.
X-Client-IP
X-Forwarded-For
(Header may return multiple IP addresses in the format: "client IP, proxy 1 IP, proxy 2 IP", so we take the the first one.)CF-Connecting-IP
(Cloudflare)True-Client-Ip
(Akamai and Cloudflare)X-Real-IP
(Nginx proxy/FastCGI)X-Cluster-Client-IP
(Rackspace LB, Riverbed Stingray)X-Forwarded
, Forwarded-For
and Forwarded
(Variations of #2)req.connection.remoteAddress
req.socket.remoteAddress
req.connection.socket.remoteAddress
req.info.remoteAddress
If an IP address cannot be found, it will return null
.
Samples Use Cases
- Getting a user's IP for geolocation.
Running the Tests
Make sure you have the necessary dev dependencies needed to run the tests:
npm install
Run the integration tests
npm test
Release Notes
See the wonderful changelog
To easily generate a new changelog, install github-changelog-generator then run npm run changelog
.
Contributors
- Thanks to @osherx for adding the connect-middleware.
- Thanks to @raunc for adding Squid proxy support.
- Thanks to @fluxsauce for adding
CF-Connecting-IP
, True-Client-IP
, and ES6 support.
License
The MIT License (MIT) - 2017