Node.js

node.jsでローカルIPアドレスの取得

More than 3 years have passed since last update.

メモ


js

var os = require('os');

console.log(getLocalAddress());

function getLocalAddress() {
var ifacesObj = {}
ifacesObj.ipv4 = [];
ifacesObj.ipv6 = [];
var interfaces = os.networkInterfaces();

for (var dev in interfaces) {
interfaces[dev].forEach(function(details){
if (!details.internal){
switch(details.family){
case "IPv4":
ifacesObj.ipv4.push({name:dev, address:details.address});
break;
case "IPv6":
ifacesObj.ipv6.push({name:dev, address:details.address})
break;
}
}
});
}
return ifacesObj;
};