Skip to content

Instantly share code, notes, and snippets.

@muthu32
Created March 3, 2025 13:20
Show Gist options
  • Save muthu32/88468e48a48d8d544c2ccbaf06ff3dd0 to your computer and use it in GitHub Desktop.
Save muthu32/88468e48a48d8d544c2ccbaf06ff3dd0 to your computer and use it in GitHub Desktop.
Ip address get in pure nodejs
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