deltanovember deltanovember - 3 months ago 11
Node.js Question

How can I get the local IP address in Node.js?

I'm not referring to

127.0.0.1

But rather the one that other computers would use to access the machine e.g.

192.168.1.6

Answer

http://nodejs.org/api/os.html#os_os_networkinterfaces

var os = require('os');

var interfaces = os.networkInterfaces();
var addresses = [];
for (var k in interfaces) {
    for (var k2 in interfaces[k]) {
        var address = interfaces[k][k2];
        if (address.family === 'IPv4' && !address.internal) {
            addresses.push(address.address);
        }
    }
}

console.log(addresses);
Comments