#request-ip
A tiny Node.js module for retrieving a request's IP address.
npm install request-ip --save
var requestIp = require('request-ip');
// inside middleware handler
var ipMiddleware = function(req, res, next) {
var clientIp = requestIp.getClientIp(req);
next();
};
// on localhost you'll see 127.0.0.1 if you're using IPv4
// or ::1, ::ffff:127.0.0.1 if you're using IPv6
var requestIp = require('request-ip');
app.use(requestIp.mw())
app.use(function(req, res) {
var 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.
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.X-Real-IP
(nginx proxy/FastCGI)X-Cluster-Client-IP
(Rackspace LB, Riverbed Stingray)- Permuations of #2 such as:
X-Forwarded
,Forwarded-For
andForwarded
req.connection.remoteAddress
req.socket.remoteAddress
req.connection.socket.remoteAddress
req.info.remoteAddress
If cannot find an IP address, it will return null
.
- Getting a user's IP for geolocation.
None
Make sure you have the necessary dev dependencies needed to run the tests:
npm install
Run the integration tests
npm test
See the wonderful changelog
To easily generate a new changelog, install github-changelog-generator then run npm run changelog
.
Thanks to @osherx for adding the connect-middleware.
The MIT License (MIT) - 2016