Created
March 3, 2025 13:20
-
-
Save muthu32/88468e48a48d8d544c2ccbaf06ff3dd0 to your computer and use it in GitHub Desktop.
Ip address get in pure nodejs
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"); | |
function getLocalIPAddress() { | |
const interfaces = os.networkInterfaces(); | |
for (const name of Object.keys(interfaces)) { | |
for (const iface of interfaces[name]) { | |
if (iface.family === "IPv4" && !iface.internal) { | |
return iface.address; | |
} | |
} | |
} | |
return "127.0.0.1"; // Default to localhost if no external address is found | |
} | |
module.exports = { getLocalIPAddress }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment