How to Get IP Address in Node Js?

13-Sep-2022

.

Admin

How to Get IP Address in Node Js?

Hello Friends,

This example is focused on how to get the IP address in node js. it's a simple example of how to get ip address in node.js. this example will help you get ip address in nodejs. I explained simply step by step to get an IP address in node js.

I will give you a simple example of get ip address in node js. In this example get ip address using npm install ip package in node js. There are multiple ways to get ip address in Nodejs.

Every device online is identified with a string of numbers called an IP address. The IP address is a request's IP address that is useful for a number of things, like preventing fraud or providing targeted location services to your users.

So let's start following example with output:

Step 1: Install Node JS


This step is not required; however, if you have not created the node js app, then you may go ahead and execute the below command:

mkdir my-app

cd my-app

npm init

Step 2: Install IP Address Package

npm install ip --save

Step:3 Update server.js file

server.js

//import express package show ip address:

const express = require('express')

const app = express()

const port = 3000

const IP = require('ip');

//get route show ip address

app.get('/', (req, res) => {

const ipAddress = IP.address();

res.send(ipAddress)

})

//run port get ip address output:

app.listen(port, () => {

console.log(`Example app listening on port ${port}`)

})

Run Node App

node serevr.js

Open your browser and write this URL:

http://localhost:3000/

Output:

I hope it can help you...

#Node.js Express

#Node JS