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

JavaScript Node.js

卡卡西达蒙西里

2020-03-19

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

第2207篇《在node.js中获取本地IP地址》来自Winter(https://github.com/aiyld/aiyld.github.io)的站点

3个回答
宝儿猪猪 2020.03.19

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 
});
飞云西门Near 2020.03.19

Install a module called ip like

npm install ip

then use this code.

var ip = require("ip");
console.log( ip.address() );
梅十三 2020.03.19

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

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

问题类别

JavaScript Ckeditor Python Webpack TypeScript Vue.js React.js ExpressJS KoaJS CSS Node.js HTML Django 单元测试 PHP Asp.net jQuery Bootstrap IOS Android