知识库

个人知识管理

用户工具

站点工具


web:nodejs获得本机ip地址

nodejs获得本机IP地址

'use strict';
 
var os = require('os');
var ifaces = os.networkInterfaces();
 
Object.keys(ifaces).forEach(function (ifname) {
 var alias = 0;
 
 ifaces[ifname].forEach(function (iface) {
   if ('IPv4' !== iface.family || iface.internal !== false) {
     // skip over internal (i.e. 127.0.0.1) and non-ipv4 addresses
     return;
   }
 
   if (alias >= 1) {
     // this single interface has multiple ipv4 addresses
     console.log(ifname + ':' + alias, iface.address);
   } else {
     // this interface has only one ipv4 adress
     console.log(ifname, iface.address);
   }
   ++alias;
 });
});
web/nodejs获得本机ip地址.txt · 最后更改: 2019/09/11 00:49 (外部编辑)