- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Express.js – req.ips Property
The req.ips property contains an array of all the IP addresses in the X-Forwarded-For request header value. This property is only populated when the trust proxy setting does not evaluate to False. This header values or IP's can be set either by the proxy or the client.
Syntax
req.ips
Example 1
Create a file with the name "reqIps.js" and copy the following code snippet. After creating the file, use the command "node reqIps.js" to run this code as shown in the example below −
// req.ips Property Demo Example // Importing the express & cookieParser module var cookieParser = require('cookie-parser'); var express = require('express'); // Initializing the express and port number var app = express(); // Initializing the router from express var router = express.Router(); var PORT = 3000; // Defining multiple Endpoint app.get('/api', function (req, res) { console.log("IP's : ", req.ips); res.send(req.ips); }); app.listen(PORT, function(err){ if (err) console.log(err); console.log("Server listening on PORT", PORT); });
Hit the following Endpoint with a GET request: localhost:3000/api
Output
C:\home
ode>> node reqIps.js Server listening on PORT 3000 IP's : []
Example 2
Let's take a look at one more example.
// req.ips Property Demo Example // Importing the express & cookieParser module var cookieParser = require('cookie-parser'); var express = require('express'); // Initializing the express and port number var app = express(); // Initializing the router from express var router = express.Router(); var PORT = 3000; // Defining multiple Endpoint app.get('/api', function (req, res) { console.log("IP's : ", req.ips); res.send(req.ips); }); app.listen(PORT, function(err){ if (err) console.log(err); console.log("Server listening on PORT", PORT); });
Output
Hit the following Endpoint one by one −
GET Request – localhost:3000/api
and set the header as −
x-forwarded-for: 105.0.114.165, 61.91.3.17, 120.192.192.255
C:\home
ode>> node reqIps.js Server listening on PORT 3000 Server listening on PORT 3000 IP's : [105.0.114.165, 61.91.3.17, 120.192.192.255]
Advertisements