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
4 Answers
Reset to default 4const { 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
版权声明:本文标题:javascript - How to get My PUBLIC IP address Using Node js - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745162286a2645491.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论