node.js获取当前IP地址

以下函数可实现该功能:
function getLocalIP() {
const os = require(‘os’);
const ifaces = os.networkInterfaces();
let locatIp = ‘’;
for (let dev in ifaces) {
if (dev === ‘本地连接’) {
for (let j = 0;j < ifaces[dev].length;j++) {
if (ifaces[dev][j].family === ‘IPv4’) {
locatIp = ifaces[dev][j].address;
break;
}
}
}
}
return locatIp;
}

你可能感兴趣的:(node.js)