如何检查地址是否是环回地址nodejs

时间:2016-03-03 15:50:39

标签: javascript node.js networking

我试图编写一个函数来返回我网络中的第一个非环回ipv4地址。我可以做任何事情,除了检查一个地址是否是一个环回地址有没有办法做这个nodejs?这是我的功能:

function externalIP(){
  var addrs = os.networkInterfaces();
  var address = ""

  for(var key in addrs){
    for (var k in addrs[key]) {
      if(addrs[key][k].family == "IPv4"){
        console.log(addrs[key][k].address);
        /* HERE I NEED TO CHECK IF THE ADDRESS IS A LOOPBACK ADDRESS */
        address = addrs[key][k].address
        break
      }
    }
    // check if address is already set we can return the result
    if (address != ""){
      break
    }
  }

  return address
}

1 个答案:

答案 0 :(得分:0)

我最终只是在做一个基于正则表达式的解决方案:

public class Canvas : NSView
{
    public Canvas (CGRect rect) : base(rect)
    {
        WantsLayer = true; 
        Layer.BackgroundColor = NSColor.Clear.CGColor;
        Layer.MasksToBounds = true;
        Layer.NeedsDisplayOnBoundsChange = true;
        LayerUsesCoreImageFilters = true; 
    }

    CIFilter effect; 
    public CIFilter Effect {
        get { return effect; }
        set{ 
            effect = value;
            Layer.Filters = new CIFilter[1]{Effect}; 
            NeedsDisplay = true; 
        }
    }
}