Node js Express Get Client IP Address Tutorial

Node js Express Get Client IP Address Tutorial

In this tutorial, you will learn how to get the client IP address in node express js application using request ip.

A small node.js module to retrieve the request’s IP address, it looks for specific headers in the request and falls back to some defaults if they do not exist.

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

If an IP address cannot be found, it will return null.

How to Get Client IP Address in Node Express JS

Steps to get client IP address:

Step 1 – Create Node JS App

Run the following command on cmd to create node js app:

mkdir my-app
cd my-app
npm init -y

Step 2 – Install Express and request-ip Library

Open again your cmd and run the following command to install express and multer dependencies in your node js app:

npm install express
npm install request-ip --save

Step 3 – Create Server.js File

In this step, you need to create server.js file and add the following code to it:

var express = require('express');
var app = express();
var requestIp = require('request-ip');

The above-given code is that creates a web server using the node js Express framework to get client ip or info.

Step 4 – Create a Route to Get the IP Address

Create route in server.js file to get client IP address from the incoming HTTP request object:

var express = require('express');
var app = express();
var requestIp = require('request-ip');
      
app.get('/',function(request, response) {
  
    var clientIp = requestIp.getClientIp(request);
    console.log(clientIp);
  
});
  
app.listen(3000, () => console.log(`App listening on port 3000`))

Click to know more about the express.

Step 5 – Test Application

You can use the following command to run the development server:

//run the below command

npm start

After that, 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

AuthorAdmin

Greetings, I'm Devendra Dode, a full-stack developer, entrepreneur, and the proud owner of Tutsmake.com. My passion lies in crafting informative tutorials and offering valuable tips to assist fellow developers on their coding journey. Within my content, I cover a spectrum of technologies, including PHP, Python, JavaScript, jQuery, Laravel, Livewire, CodeIgniter, Node.js, Express.js, Vue.js, Angular.js, React.js, MySQL, MongoDB, REST APIs, Windows, XAMPP, Linux, Ubuntu, Amazon AWS, Composer, SEO, WordPress, SSL, and Bootstrap. Whether you're starting out or looking for advanced examples, I provide step-by-step guides and practical demonstrations to make your learning experience seamless. Let's explore the diverse realms of coding together.

Leave a Reply

Your email address will not be published. Required fields are marked *