Created
August 24, 2017 07:47
-
-
Save syaau/14c2f1bfa9a827eef1ede418a554c0e7 to your computer and use it in GitHub Desktop.
Retrieve the first Local IPv4 from the network interfaces
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const os = require('os'); | |
export default function getLocalIP() { | |
const ifaces = os.networkInterfaces(); | |
const ifnames = Object.keys(ifaces); | |
for (let i = 0; i < ifnames.length; i += 1) { | |
const aliases = ifaces[ifnames[i]]; | |
for (let j = 0; j < aliases.length; j += 1) { | |
const iface = aliases[j]; | |
if (iface.family === 'IPv4' && !iface.internal) { | |
// Return the first ip address found | |
return iface.address; | |
} | |
} | |
} | |
return null; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment