SORU
6 EYLÜL 2010, PAZARTESİ


Node.js yerel IP adresini almak

Basit node.js bir program benim makine üzerinde çalışan var ve programım olduğu bilgisayarın yerel IP adresi çalışan almak istiyorum. Nasıl node.js ile alabilirim?

CEVAP
9 Aralık 2011, Cuma


'use strict';

var os = require('os');
var ifaces = os.networkInterfaces();

Object.keys(ifaces).forEach(function (ifname) {
  var alias = 0
    ;

  ifaces[ifname].forEach(function (iface) {
    if ('IPv4' !== iface.family || iface.internal !== false) {
      // skip over internal (i.e. 127.0.0.1) and non-ipv4 addresses
      return;
    }

    if (alias >= 1) {
      // this single interface has multiple ipv4 addresses
      console.log(ifname   ':'   alias, iface.address);
    } else {
      // this interface has only one ipv4 adress
      console.log(ifname, iface.address);
    }
  });
});

// en0 192.168.1.101
// eth0 10.0.0.101

Bunu Paylaş:
  • Google+
  • E-Posta
Etiketler:

YORUMLAR

SPONSOR VİDEO

Rastgele Yazarlar

  • engineerguy

    engineerguy

    10 Ocak 2010
  • Mega64

    Mega64

    24 ŞUBAT 2006
  • MikeyMacintosh

    MikeyM

    28 Aralık 2009