Node js Express Get Client IP Address Tutorial

Node js Express Get Client IP Address Tutorial

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

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.

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

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

How to Get Client IP Address in Node JS Express

Steps to get client IP address in node js with express while the user has requested anything to server:

  • Step 1 – Create Node JS App
  • Step 2 – Install Express and request-ip Library
  • Step 3 – Create Server.js File
  • Step 4 – Import Request Ip Dependencies in Server.js File
  • Step 5 – Start Development Server

Step 1 – Create 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 Library

In this step, open again your terminal and execute 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 into it:

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

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

Here’s how it works:

  1. The first line of code var express = require('express'); imports the Express module and assigns it to the variable express. This allows the application to use the functions and features of the Express framework.
  2. The next line var app = express(); creates an instance of the Express application, which is used to define the routes and behavior of the web server.
  3. The following code block app.get('/',function(request, response) {...}) sets up a route for the root directory of the web server (i.e., the homepage). The app.get() function is an Express method that registers a route handler function that will be executed when a GET request is made to the specified path (in this case, the root path ‘/’). The function takes two parameters: request and response, which represent the incoming HTTP request and the server’s response to that request, respectively.
  4. Inside the route handler function, the requestIp.getClientIp(request) method is used to get the IP address of the client that made the request to the server. The IP address is stored in the variable clientIp.
  5. The console.log() method is used to log the client’s IP address to the console, so that the developer can see it.
  6. Finally, the code block app.listen(3000, () => console.log(App listening on port 3000)) starts the server and listens on port 3000 for incoming HTTP requests. When the server is started, the message “App listening on port 3000” is logged to the console.

Step 4 – Import Request Ip Dependencies in Server.js File

In this step, you need to import request ip dependencies in server.js file:

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

Click to know more about the express.

Step 5 – Start Development Server

You can use the following command to run development server:

//run the below command

npm start

after run this command open your browser and hit


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


My name is Devendra Dode. I am a full-stack developer, entrepreneur, and owner of I like writing tutorials and tips that can help other developers. I share tutorials of 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 from a starting stage. As well as demo example.

Leave a Reply

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