我的机器上运行了一个简单的node.js程序,我想获取运行该程序的PC的本地IP地址。如何使用node.js获得它?
在node.js中获取本地IP地址
Install a module called ip like
npm install ip
then use this code.
var ip = require("ip");
console.log( ip.address() );
https://github.com/indutny/node-ip
var ip = require("ip");
console.dir ( ip.address() );
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 executesroute.exe
ornetstat
and parses the output.