+
80
-

回答

通过os.networkInterfaces();

const os = require('os');

// 获取局域网 IP 地址
function getLocalIp() {
  const interfaces = os.networkInterfaces();
  for (const name of Object.keys(interfaces)) {
    for (const iface of interfaces[name]) {
      if (iface.family === 'IPv4' && !iface.internal) {
        return iface.address;
      }
    }
  }
  return '127.0.0.1';
}

网友回复

我知道答案,我要回答