README
get-ip-from-request
get-ip-from-request is a 0-dependency small configurable tool to get ip from http request and validate ip (v4 & v6)
Installation
Download nodejs first. Use npm to install get-ip-from-request.
npm i get-ip-from-request@latest
Usage
const { createServer } = require('http');
const { isIp, isIpv4, isIpv6, getIpFromRequestBuilder, getIpFromRequest } = require('get-ip-from-request');
const localhost = '127.0.0.1';
console.log(`${localhost} is ip ? `, isIp(localhost)); // true
console.log(`${localhost} is ip v4 ? `, isIpv4(localhost)); // true
console.log(`${localhost} is ip v6 ? `, isIpv6(localhost)); // false
const server = createServer((req, res) => {
// simple use
//const ip = getIpFromRequest(req)
// OR custum use
const options = { headers: ['my-ip-header-want-to-be-check'] }
const myPersonnalGetIpFromRequest = getIpFromRequestBuilder(options)
const ip = myPersonnalGetIpFromRequest(req)
const v4 = isIpv4(ip)
const v6 = isIpv6(ip)
const obj = { ip, v4, v6 }
return res.end(JSON.stringify(obj))
})
Benchmarks
- Machine: Windows 10 - x64 | AMD Ryzen 3 1300X Quad-Core - 3.5GHz | 8GB RAM.
- Method:
require('benchmark').Suite
. (5 rounds to measure min, max & average) - Node:
v15.10.0
- Run: Tue, 23 Feb 2021 17:17:01 GMT 2021
Version | ops/sec (average) | +-% | runs | ops/sec min | ops/sec max | |
---|---|---|---|---|---|---|
isIpv4 | 1.0.10 | 182,333 | 0.29-1.22 | 86-93 | 175,087 | 184,420 |
is.ipv4 | 0.9.0 | 186,379 | 0.22-0.47 | 90-94 | 184,396 | 188,372 |
isIpv6 | 1.0.10 | 5,373 | 0.18-0.24 | 93-96 | 5,256 | 5,407 |
is.ipv6 | 0.9.0 | 4,889 | 0.15-0.27 | 94-96 | 4,741 | 4,952 |
isIp | 1.0.10 | 5,326 | 0.18-0.37 | 90-94 | 5,262 | 5,371 |
is.ip | 0.9.0 | 4,557 | 0.14-0.18 | 94-95 | 4,480 | 4,619 |
- Machine: Windows 10 - x64 | AMD Ryzen 3 1300X Quad-Core - 3.5GHz | 8GB RAM.
- Method:
require('benchmark').Suite
. (5 rounds to measure min, max & average) - Node:
v15.10.0
- Run: Wed, 24 Feb 2021 12:36:42 GMT 2021
Version | ops/sec (average) | +-% | runs | ops/sec min | ops/sec max | |
---|---|---|---|---|---|---|
isIpv4 | 1.1.0 | 183,923 | 0.20-0.28 | 92-95 | 182,022 | 185,032 |
is.ipv4 | 0.9.0 | 183,512 | 0.27-0.33 | 89-94 | 180,761 | 185,693 |
isIpv6 | 1.1.0 | 5,792 | 0.16-0.19 | 93-96 | 5,706 | 5,864 |
is.ipv6 | 0.9.0 | 4,919 | 0.14-0.16 | 90-97 | 4,774 | 4,980 |
isIp | 1.1.0 | 5,716 | 0.17-0.26 | 93-96 | 5,628 | 5,764 |
is.ip | 0.9.0 | 4,572 | 0.16-0.28 | 93-96 | 4,468 | 4,615 |