1

通过引入os模块获取系统信息

var os = require('os');
var serverIP = getIPAddress();
console.log('serverIP', serverIP);

// 获取内网ip
function getIPAddress() {
  let IPAddress = '';
  var interfaces = os.networkInterfaces();
  for (var devName in interfaces) {
    var iface = interfaces[devName];
    for (var i = 0; i < iface.length; i++) {
      var alias = iface[i];
      if (alias.family === 'IPv4' && alias.address !== '127.0.0.1' && !alias.internal) {
        IPAddress = alias.address;
      }
    }
  }
  return IPAddress;
};

ZHAO_
449 声望11 粉丝

前端开发