Node js Express Get Client IP Address Tutorial

Getting the client IP address in a Node.js Express application is a common task. Here is a simple tutorial on how to do it.

1. Install the ip-address npm package

The first step is to install the ip-address npm package. This package provides a simple way to get the client IP address from a Node.js Express application.

To install the package, open a terminal window and run the following command:

npm install ip-address

2. Require the ip-address package

Once the package is installed, you can require it in your Node.js Express application.

To do this, add the following line of code to the top of your application:

const ip = require(‘ip-address’);

3. Get the client IP address

Now that the package is installed and required, you can get the client IP address.

To do this, add the following line of code to your application:

const clientIp = ip.address();

This will return the client IP address as a string.

4. Use the client IP address

Now that you have the client IP address, you can use it in your application.

For example, you could use it to log the IP address of each user that visits your application.

Conclusion

In this tutorial, we have shown you how to get the client IP address in a Node.js Express application. We have also shown you how to use the client IP address in your application.

Node js express get 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
  12. req.info.remoteAddress

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

How to Get Client IP Address in Node JS

  • 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();
      
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 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 

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

Jaspreet Singh Ghuman

Jaspreet Singh Ghuman

Jassweb.com/

Passionate Professional Blogger, Freelancer, WordPress Enthusiast, Digital Marketer, Web Developer, Server Operator, Networking Expert. Empowering online presence with diverse skills.

jassweb logo

Jassweb always keeps its services up-to-date with the latest trends in the market, providing its customers all over the world with high-end and easily extensible internet, intranet, and extranet products.

Contact
San Vito Al Tagliamento 33078
Pordenone Italy
Item added to cart.
0 items - 0.00
Open chat
Scan the code
Hello 👋
Can we help you?