Created
January 16, 2022 22:41
-
-
Save BaileySimrell/bd37c9e34f483f3e52409f28bebb3704 to your computer and use it in GitHub Desktop.
Retrieve local IP address with Node JS
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
'use strict'; | |
const { networkInterfaces } = require('os'); | |
const nets = networkInterfaces(); | |
const results = Object.create(null); // Or just '{}', an empty object | |
for (const name of Object.keys(nets)) { | |
for (const net of nets[name]) { | |
// Skip over non-IPv4 and internal (i.e. 127.0.0.1) addresses | |
if (net.family === 'IPv4' && !net.internal) { | |
if (!results[name]) { | |
results[name] = []; | |
} | |
results[name].push(net.address); | |
} | |
} | |
} | |
console.log(results) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment