admin管理员组

文章数量:1416314

I want to get my Public IP address using Node js only

For example when i search on Google "What is my Ip address "

Gives me output like 10X.XX.XX.XXX

How to get this Ip address Using node js without importing any Libraries like ip , Public-ip etc ...

I want to write my Custom Code to get public ip Without sending requests to other websites like whatismyipaddress , ipconfig etc ...

I want to get my Public IP address using Node js only

For example when i search on Google "What is my Ip address "

Gives me output like 10X.XX.XX.XXX

How to get this Ip address Using node js without importing any Libraries like ip , Public-ip etc ...

I want to write my Custom Code to get public ip Without sending requests to other websites like whatismyipaddress. , ipconfig. etc ...

Share Improve this question edited Feb 6, 2022 at 9:49 VLAZ 29.2k9 gold badges63 silver badges84 bronze badges asked Feb 6, 2022 at 9:45 tech lazytech lazy 191 silver badge5 bronze badges 5
  • 1 You can check this question stackoverflow./q/20273128/5146848 – Sifat Haque Commented Feb 6, 2022 at 9:52
  • Does this answer your question? How to get client's IP address using JavaScript? – Steven Spungin Commented Feb 6, 2022 at 9:59
  • @StevenSpungin No , I want to write my Custom code to get Public Ip address – tech lazy Commented Feb 6, 2022 at 10:25
  • @SifatHaque using those method i am getting my wifi-->properties-->IPv4 address not my Public IP address which i can view when i search on google "what is my Ip address " – tech lazy Commented Feb 6, 2022 at 10:26
  • 1 You need to connect to a remote site as stated. The URLS in that link do just that. Public IP is same as client IP, no? Do you want your domain name? – Steven Spungin Commented Feb 6, 2022 at 10:37
Add a ment  | 

4 Answers 4

Reset to default 4
const { exec } = require('child_process')

exec('curl ip-adresim.app', function(error, stdout, stderr){
    if(error)
        return;
    console.log('your ip is :'+ stdout);
})

you could run a mand and get the output like curl or ping

There is no native way to get the public IP, in fact, there is no way to get it from your server, not even from the Linux mand, you have to ask an external server, and this package does exactly so with different servers.

I understand you don't want to ask an external library or an external server, but you have no choice.

Im suggesting using an external library, because it takes care of asking multiple servers instead of just one in the case that one goes offline

You can try this package it works perfectly

Here's a native JS request method:

fetch("https://ipecho.io/json")
  .then((response) => response.json())
  .then((data) => console.log(data.ip));

The access method encapsulated as a function is:

async function myip() {
  const res = await fetch("https://ipecho.io/json");
  const data = res.json();
  return data.ip;
}

const ip = await myip();
console.log(ip);

using promises,

async function myip() {
  return new Promise((resolve,reject) => {
    fetch('https://ipecho.io/json')
    .then(res => res.json())
    .then(res => resolve(res.ip))
    .catch(err => reject(err));        
  })
}

then invoke,

const ip = await myip();

本文标签: javascriptHow to get My PUBLIC IP address Using Node jsStack Overflow