Node js express get client ip address example; In this tutorial, i am going to show you how to get User’s/client IP address in Node.js (Express.js)
An IP address, or Internet Protocol address, is a series of numbers that identifies any device on a network. Computers use IP addresses to communicate with each other both over the internet as well as on other networks.
The user IP is determined by the following order:
X-Client-IPX-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.)CF-Connecting-IP(Cloudflare)Fastly-Client-Ip(Fastly CDN and Firebase hosting header when forwared to a cloud function)True-Client-Ip(Akamai and Cloudflare)X-Real-IP(Nginx proxy/FastCGI)X-Cluster-Client-IP(Rackspace LB, Riverbed Stingray)X-Forwarded,Forwarded-ForandForwarded(Variations of #2)req.connection.remoteAddressreq.socket.remoteAddressreq.connection.socket.remoteAddressreq.info.remoteAddress
Get User’s/Client IP Address in Node JS Express
- Step 1 - Create New Node JS App
- Step 2 - Install Express and request-ip Module
- Step 3 - Create Server.js File
- Step 4 - Import Request Ip Module in Server.js File
- Step 5 - Start App Server
Step 1 - Create New Node JS App
Create new node js app; execute the following command on terminal to install node js app:
mkdir my-app cd my-app npm init -y
Step 2 - Install Express and request-ip Module
Install experss and request-ip module; eecute the following command to install express and ip-address dependencies in your node js app:
npm install express npm install request-ip --save
Step 3 - Create Server.js File
Create server.js file and add the following code into it:
var express = require('express');
var app = express();
app.get('/',function(request, response) {
var clientIp = requestIp.getClientIp(request);
console.log(clientIp);
});
app.listen(3000, () => console.log(`App listening on port 3000`))
Step 4 - Import Request Ip Module in Server.js File
Import request ip module in server.js file:
var requestIp = require('request-ip');
Step 5 - Start App Server
Start app server; execute the following command on terminal:
//run the below command npm start after run this command open your browser and hit http://127.0.0.1:3000/
Conclusion
Node js express get client ip address example. In this tutorial, you have learned how to get client ip address in node js and express js using request ip.