pbojinov/request-ip
关于
一个用于在服务器端获取请求 IP 地址的 Node.js 模块。
开发工具实测星标增长
- 24小时
- 0
- 7天
- +1
- 15天
- +1
- 1个月
- 正在收集历史
各周期内的星标净变化,周期相互重叠,并非每日新增。缺失的测量保持空白。星标代表关注度,不代表软件质量。
README.md
原文request-ip
A tiny Node.js module for retrieving a request's IP address.

Installation
Yarn
yarn add request-ip
npm
npm install request-ip --save
Getting Started
const requestIp = require('request-ip');
// inside middleware handler
const ipMiddleware = function(req, res, next) {
const 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
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 user ip is determined by the following order:
X-Client-IPX-Forwarded-For(Header may return multiple IP addresses in the format: "client IP, proxy 1 IP, proxy 2 IP", so we take the first one.)CF-Connecting-IP(Cloudflare)Fastly-Client-Ip(Fastly CDN and Firebase hosting header when forwared to a cloud function)True-Client-Ip(Akamai and Cloudflare)X-Real-IP(Nginx proxy/FastCGI)X-Cluster-Client-IP(Rackspace LB, Riverbed Stingray)X-Forwarded,Forwarded-ForandForwarded(Variations of #2)appengine-user-ip(Google App Engine)req.connection.remoteAddressreq.socket.remoteAddressreq.connection.socket.remoteAddressreq.info.remoteAddressCf-Pseudo-IPv4(Cloudflare fallback)request.raw(Fastify)
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
Building
Compiles the current ES6 code to ES5 using Babel.
npm build
Release Notes
See the wonderful changelog
To generate a new changelog, install github-changelog-generator then run npm run changelog. This will require being on Ruby >= 3
Contributors
Thank you to all the contributors!
License
The MIT License (MIT) - 2022
探索相关项目
加载中…