Created
June 20, 2022 07:29
-
-
Save productdevbook/d144b69ce6376b62abb7f8812321b60b to your computer and use it in GitHub Desktop.
Get IP Adress Func
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
import os from 'node:os' | |
function getIPAdress() { | |
let localIPAddress = '' | |
const interfaces = os.networkInterfaces() | |
for (const devName in interfaces) { | |
const iface = interfaces[devName] as os.NetworkInterfaceInfo[] | |
for (let i = 0; i < iface.length; i++) { | |
const alias = iface[i] | |
if ( | |
alias.family === 'IPv4' && | |
alias.address !== '127.0.0.1' && | |
!alias.internal | |
) { | |
localIPAddress = alias.address | |
} | |
} | |
} | |
return localIPAddress | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment