在node.js中获取本地IP地址

我的机器上运行了一个简单的node.js程序,我想获取运行该程序的PC的本地IP地址。如何使用node.js获得它?

宝儿猪猪2020/03/19 09:28:29

I wrote a Node.js module that determines your local IP address by looking at which network interface contains your default gateway.

This is more reliable than picking an interface from os.networkInterfaces() or DNS lookups of the hostname. It is able to ignore VMware virtual interfaces, loopback, and VPN interfaces, and it works on Windows, Linux, Mac OS, and FreeBSD. Under the hood, it executes route.exe or netstat and parses the output.

var localIpV4Address = require("local-ipv4-address");

localIpV4Address().then(function(ipAddress){
    console.log("My IP address is " + ipAddress);
    // My IP address is 10.4.4.137 
});
飞云西门Near2020/03/19 09:28:29

Install a module called ip like

npm install ip

then use this code.

var ip = require("ip");
console.log( ip.address() );
梅十三2020/03/19 09:28:29

https://github.com/indutny/node-ip

var ip = require("ip");
console.dir ( ip.address() );