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

sureshramani

Suresh Ramani

Posted on September 18, 2022

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

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-Forwarded, Forwarded-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
Enter fullscreen mode Exit fullscreen mode

Read Also: 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
Enter fullscreen mode Exit fullscreen mode

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}`)
})
Enter fullscreen mode Exit fullscreen mode

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}`)
})
Enter fullscreen mode Exit fullscreen mode

Step 5: Start Development Server

Execute the following command to run the development server:

node server.js
Enter fullscreen mode Exit fullscreen mode

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

Thank you for reading this article.

💖 💪 🙅 🚩
sureshramani
Suresh Ramani

Posted on September 18, 2022

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related