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 to retrieve a request's IP address.
Maintainer: Petar Bojinov
Installation
npm install request-ip
Getting Started
var requestIp = require('request-ip');
var ipMiddleware = function(req, res, next) {
var clientIp = requestIp.getClientIp(req);
next();
};
How It Works
request-ip looks for three specific headers in the request and falls back 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.X-Real-IP
X-Cluster-Client-IP
- Permuations of #2 such as:
X-Forwarded
, Forwarded-For
and Forwarded
req.connection.remoteAddress
req.socket.remoteAddress
req.connection.socket.remoteAddress
Use Case
Getting a user's IP for geolocation.
Dependencies
None
Running the Tests
Make sure you have the necessary dependencies:
npm install
Run the integration tests
npm test
Release Notes
1.1.0
- add support for X-Cluster-Client-IP, X-Forwarded, Forwarded-For, Forwarded
- add tests
1.0.0
0.0.4
- add support for X-Real-Ip
- bug fixes
0.0.3
0.0.2
0.0.1
License
The MIT License (MIT)