Last Updated: February 25, 2016
·
524
· tralamazza

(I might go to hell for this) way of listing local IPs

var os = require('os');

function localIPs(custom_filter) {
  /* default: only IPv4 and non internal */
  custom_filter = custom_filter || function (i) { return i.family === 'IPv4' && !i.internal; };
  var ni = os.networkInterfaces();
  return Array.prototype.concat.apply(Array.prototype,
    Object.keys(ni)
      .filter(function (k) { return k !== 'lo'; }) /* ignore loopback */
      .map(function (k) { return ni[k]; }) /* get value */
    ) /* flatten */
    .filter(custom_filter)
    .map(function (a) { return a.address; }); /* get address */
}

console.log(localIPs());