Home > Blockchain >  Node.js get a computer network ip format usable in an external LAN post request
Node.js get a computer network ip format usable in an external LAN post request

Time:02-20

The code below is built to fetch the network ip of a computer in a LAN network.

I need to identify the computer's network ip format like '192.1.xx.x.x59' that can be usable when making an external LAN post request.

The problem is the code keeps returning '0.0.0.0' as the network ip.

Could you please help me spot the problem with this code.

Thanks in advance!


function getIPAddress() {
    var interfaces = require('os').networkInterfaces();
    for (var devName in interfaces) {
      var iface = interfaces[devName];
  
      for (var i = 0; i < iface.length; i  ) {
        var alias = iface[i];
        if (alias.family === 'IPv4' && alias.address !== '127.0.0.1' && !alias.internal)
          return alias.address;
      }
    }
    return '0.0.0.0';
}

console.log(getIPAddress());

CodePudding user response:

According to your code, I think you didn't connect to any network wifi or something. So, returning 0.0.0.0

you can see by logging interfaces

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

console.log(interfaces);
  • Related