Skip to content

Instantly share code, notes, and snippets.

@mlms13
Created June 3, 2014 15:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mlms13/67696210ce621dd10152 to your computer and use it in GitHub Desktop.
Save mlms13/67696210ce621dd10152 to your computer and use it in GitHub Desktop.
Find IP address in node
var getIpAddress = function (interfaces) {
'use strict';
var ip = [],
iface = null;
// loop through each interface in the interfaces object
for (iface in interfaces) {
// loop through each of the objects in the iface array
interfaces[iface].forEach(function (i) {
// only keep track of non-internal IPv4 addresses
if(i.family === 'IPv4' && !i.internal) {
ip.push(i);
}
});
}
// warn if multiple matches were found
if (ip.length > 1) {
console.warn('Multiple IP addresses found. Returning only the first one.');
}
return ip[0].address;
};
// usage
getIpAddress(require('os').networkInterfaces());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment