简体   繁体   English

"如何使用 Node js 获取我的公共 IP 地址"

[英]How to get My PUBLIC IP address Using Node js

I want to get my Public IP address using Node js only我只想使用 Node js 获取我的公共 IP 地址

For example when i search on Google "What is my Ip address "例如,当我在 Google 上搜索“我的 IP 地址是什么”时

Gives me output like 10X.XX.XX.XXX给我像 10X.XX.XX.XXX 这样的输出

How to get this Ip address Using node js without importing any Libraries like ip , Public-ip etc ...如何在不导入任何库(如 ip 、 Public-ip 等)的情况下使用节点 js 获取此 IP 地址...

I want to write my Custom Code to get public ip Without sending requests to other websites like whatismyipaddress.com , ipconfig.com etc ...我想编写我的自定义代码来获取公共 ip 而不向其他网站发送请求,如 whatismyipaddress.com 、 ipconfig.com 等......

"

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 command and get the output like curl or ping您可以运行命令并获取 curl 或 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 command, you have to ask an external server, and this package does exactly so with different servers.没有本地获取公共 IP 的方法,事实上,没有办法从您的服务器获取它,甚至无法从 Linux 命令获取,您必须询问外部服务器,而这个包在不同的服务器上正是这样做的。

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<\/a> it works perfectly你可以试试这个包<\/a>它完美地工作

"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM