Last active
November 5, 2015 03:43
-
-
Save aleung/acfa5ca91c8de6764280 to your computer and use it in GitHub Desktop.
Get local IP address of specific NIC in node.js
This file contains 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
var _ = require('lodash'); | |
var os = require('os'); | |
function getIpAddr(nicName) { | |
let ifaces = os.networkInterfaces(); | |
let iface = _.find(ifaces[nicName], iface => { return iface.family === 'IPv4' }); | |
return _.result(iface, 'address'); | |
} | |
module.exports = _.memoize(getIpAddr); |
This file contains 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
var ip = require('./ipaddr'); | |
console.log(ip('eth0')); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment