How to get the IP address of a client in Node.js

How to get the IP address of a client in Node.js

In this post we will give you information about How to get the IP address of a client in Node.js. Hear we will give you detail about How to get the IP address of a client in Node.js And how to use it also give you demo for it if it is necessary.

What is an IP Address?

An Internet Protocol(IP) address also known as a logical address is given by the internet service provider(ISP) which uniquely identifies a system over the network. IP address keeps on changing from time to time.

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 in 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 first one.)
  3. CF-Connecting-IP (Cloudflare)
  4. Fastly-Client-Ip (Fastly CDN and Firebase hosting header when forwarded 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 the IP address of a client in Node.js

To get the IP address of a client, We will use the request-ip package in node.js.

Step 1: Create Node JS App

Execute the following command to create the node js app:

mkdir node-ip
cd node-ip
npm init -y

Also see: How to Install Node.js and NPM On Ubuntu 20.04

Step 2: Install Express and request-ip library

Execute the following command to install express and request-ip. Usually, We will use express as our web server.

npm install express
npm install request-ip --save

Step 3: Create a Server.js File

Create a file named server.js in your project root directory and add the following code to that file:

const express = require('express')
const app = express()
const port = 3000

app.get('/', (req, res) => {
  res.send('Hello World!')
})

app.listen(port, () => {
  console.log('Example app listening on port ${port}')
})

Step 4: Import request-ip package into node application

const express = require('express')
const requestIp = require('request-ip')
const app = express()
const port = 3000

app.get('/', (req, res) => {
  var clientIp = requestIp.getClientIp(req)
  res.send('Your IP Address is ${clientIp}.')
})

app.listen(port, () => {
  console.log('Example app listening on port ${port}')
})

Step 5: Start Development Server

Execute the following command to run the development server:

node server.js

After running the above command and open http://localhost:3000 in your browser.

Thank you for reading this article.

Also see: How to read environment variables in Node.js

  .       .

Hope this code and post will helped you for implement How to get the IP address of a client in Node.js. if you need any help or any feedback give it in comment section or you have good idea about this post you can give it comment section. Your comment will help us for help you more and improve us. we will give you this type of more interesting post in featured also so, For more interesting post and code Keep reading our blogs

For More Info See :: laravel And github

We're accepting well-written guest posts and this is a great opportunity to collaborate : Contact US