Created
May 11, 2021 08:09
-
-
Save alsotang/9c6768506e3e2325c4a2b75484982de2 to your computer and use it in GitHub Desktop.
get lan ip in node.js
This file contains 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') | |
function getLanAddress() { | |
const ifaces = os.networkInterfaces(); | |
if (ifaces) { | |
for (const dev of Object.keys(ifaces)) { | |
for (const details of ifaces[dev]) { | |
if (details.family === 'IPv4' && details.mac !== '00:00:00:00:00:00') { | |
return details.address; | |
} | |
} | |
} | |
} | |
return '127.0.0.1'; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment