Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/lete114/get-user-ip
一个轻量、小巧的Node.js模块,用于检索请求用户的IP地址 | A lightweight, small Node.js module to retrieve the IP address of the requesting user
https://github.com/lete114/get-user-ip
client-ip get-client-ip get-request-ip get-user-ip request-ip user-ip
Last synced: 14 days ago
JSON representation
一个轻量、小巧的Node.js模块,用于检索请求用户的IP地址 | A lightweight, small Node.js module to retrieve the IP address of the requesting user
- Host: GitHub
- URL: https://github.com/lete114/get-user-ip
- Owner: Lete114
- License: mit
- Created: 2021-12-10T12:06:38.000Z (almost 3 years ago)
- Default Branch: main
- Last Pushed: 2024-05-21T15:29:48.000Z (6 months ago)
- Last Synced: 2024-09-19T10:17:50.654Z (about 2 months ago)
- Topics: client-ip, get-client-ip, get-request-ip, get-user-ip, request-ip, user-ip
- Language: JavaScript
- Homepage: https://get-user-ip.vercel.app
- Size: 35.2 KB
- Stars: 10
- Watchers: 1
- Forks: 2
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
Get-User-IP
A lightweight, small Node.js module to retrieve the IP address of the requesting user
## Installation
```bash
npm install get-user-ip --save
```## Getting Started
```javascript
const GetUserIP = require('get-user-ip')
const http = require('http')const server = http.createServer((req,res)=>{
res.end(GetUserIP(req))
})server.listen(6870)
// 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
```If there are some special cases, such as the use of `CloudFlare`, you can append a second parameter, which is an array so it can contain more than one params
```javascript
const server = http.createServer((req,res)=>{
// This gives priority to getting headers.cf-connecting-ip, and if it doesn't exist, continue with the default parameters
res.end(GetUserIP(req,['headers.cf-connecting-ip']))
})
```## How It Works
It looks for a specific header in the request and returns the `0.0.0.0` default if it does not exist
The user IP is determined by the following order
```javascript
const defaultHeaders = [
'headers.x-client-ip',
'headers.x-real-ip',
'headers.x-forwarded-for', // This header will return multiple IP addresses, Format: (Client IP, Proxy 1 IP, Proxy 2 IP...) So return the first
'connection.remoteAddress',
'socket.remoteAddress',
'connection.socket.remoteAddress'
]
```