使用 JavaScript 检测本机的局域网IP地址
function getLocalIp() {
var ifaces = require("os").networkInterfaces();
for (var dev in ifaces) {
var iface = ifaces[dev].filter((details) => {
return (
details.family === "IPv4" && // 只考虑IPv4
details.internal === false && // 过滤掉回环地址
!details.mac.startsWith("00:50:56") //过滤掉VMware的网卡
);
});
if (iface.length > 0) {
address = iface[0].address;
return address;
}
}
}
module.exports = {
getLocalIp
};
参考: