获取node.js中的本地IP地址 [英] Get local IP address in node.js
本文介绍了获取node.js中的本地IP地址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在我的机器上运行了一个简单的node.js程序,我希望得到运行我程序的PC的本地IP地址。如何使用node.js获取它?
I have a simple node.js program running on my machine and I want to get local IP address of PC on which is my program running. How do I get it with node.js?
推荐答案
'use strict';
var os = require('os');
var ifaces = os.networkInterfaces();
Object.keys(ifaces).forEach(function (ifname) {
var alias = 0;
ifaces[ifname].forEach(function (iface) {
if ('IPv4' !== iface.family || iface.internal !== false) {
// skip over internal (i.e. 127.0.0.1) and non-ipv4 addresses
return;
}
if (alias >= 1) {
// this single interface has multiple ipv4 addresses
console.log(ifname + ':' + alias, iface.address);
} else {
// this interface has only one ipv4 adress
console.log(ifname, iface.address);
}
++alias;
});
});
// en0 192.168.1.101
// eth0 10.0.0.101
这篇关于获取node.js中的本地IP地址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文