Express middleware to parse ip address and device information from request
npm install ip-device-parserIP Adress and Client Device Information from the request and it will hold following data.
sh
$ npm install ip-device-parser
`
API
`js
const express = require('express');
const ipAndDeviceParser = require('ip-device-parser');
const app = express();
app.use(ipAndDeviceParser());
`
You can fetch ip and device infor form req.clientInfo where req is express request object
`js
// req.clientInfo
{
agent: {
browser: {
name: 'Chrome Mobile',
version: '67.0.3396'
},
device: {
name: 'OnePlus ONEPLUS A5000',
version: '0.0.0'
},
os: {
name: 'Android',
version: '8.1.0'
}
},
ip: '2xx5:2x5:1xx6:2xx3:2xxc:dxx3:7xx2:6xxa'
}
`
How It Works
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.
1. X-Client-IP
2. 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.)
3. CF-Connecting-IP (Cloudflare)
4. True-Client-Ip (Akamai and Cloudflare)
5. X-Real-IP (Nginx proxy/FastCGI)
6. X-Cluster-Client-IP (Rackspace LB, Riverbed Stingray)
7. X-Forwarded, Forwarded-For and Forwarded (Variations of #2)
8. req.connection.remoteAddress
9. req.socket.remoteAddress
10. req.connection.socket.remoteAddress
11. req.info.remoteAddress
If an IP address cannot be found, it will return null`.