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

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

In this article, You will learn How to get the IP address of a client in Node.js.


Suresh Ramani - Author - TechvBlogs
Suresh Ramani
 

2 months ago

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

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

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.

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

If you want to manage your VPS / VM Server without touching the command line go and  Checkout this linkServerAvatar allows you to quickly set up WordPress or Custom PHP websites on VPS / VM in a  matter of minutes.  You can host multiple websites on a single VPS / VM, configure SSL certificates, and monitor the health of your server without ever touching the command line interface.

Comments (0)

ServerAvatar
Multiple sites on a single VPS/VM/Server Deploy PHP sites on cloud in minutes, simple as that!
ServerAvatar
Generate Free SSL Certificate - SSLForWeb

Generate Free SSL Certificate

  • 90-Day Certificates
  • SSL Monitoring
  • Wildcard Certificates
  • Multiple Domains
  • Certificate Management
  • Expiration Reminders

Comment


Note: All Input Fields are required.