1. get-user-ip
一个轻量、小巧的Node.js模块,用于检索请求用户的IP地址 | A lightweight, small Node.js module to retrieve the IP address of the requesting user
get-user-ip
Package: get-user-ip
Created by: Lete114
Last modified: Fri, 15 Dec 2023 11:55:53 GMT
Version: 1.0.2
License: MIT
Downloads: 1,285
Repository: https://github.com/Lete114/Get-User-IP

Install

npm install get-user-ip
yarn add get-user-ip
Language: English 中文

Get-User-IP

A lightweight, small Node.js module to retrieve the IP address of the requesting user

Version dev MIT License

Installation

 npm install get-user-ip --save

Getting Started

 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

 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

 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'
]

Dependencies

RELATED POST

Enhancing Vue.js Development: Harnessing the Potential of Vue-Loader

Enhancing Vue.js Development: Harnessing the Potential of Vue-Loader

Simplify Data Validation in Vue.js: A Step-by-Step Guide to Using Regex

Simplify Data Validation in Vue.js: A Step-by-Step Guide to Using Regex

Troubleshooting Made Easy: Common Issues and Solutions with vue-loader Without vue-cli

Troubleshooting Made Easy: Common Issues and Solutions with vue-loader Without vue-cli

Optimizing Webpack 4 with Vue CLI 3: Disabling the Cache-Loader

Optimizing Webpack 4 with Vue CLI 3: Disabling the Cache-Loader

Step-by-Step Guide: How to Add a Function to Your Vuex Plugin

Step-by-Step Guide: How to Add a Function to Your Vuex Plugin