Node js Express Get Client IP Address Example

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:

  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. Fastly-Client-Ip (Fastly CDN and Firebase hosting header when forwared to a cloud function)
  5. True-Client-Ip (Akamai and Cloudflare)
  6. X-Real-IP (Nginx proxy/FastCGI)
  7. X-Cluster-Client-IP (Rackspace LB, Riverbed Stingray)
  8. X-ForwardedForwarded-For and Forwarded (Variations of #2)
  9. req.connection.remoteAddress
  10. req.socket.remoteAddress
  11. req.connection.socket.remoteAddress
  12. req.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.

Recommended Node JS Tutorials

Be First to Comment

Leave a Reply

Your email address will not be published.