最简的方法,安装ip模块:npm install --save ip
添加两句:
var ip = require('ip');
var myip = ip.address(); // 本机IP
其它方法:
var os=require('os');
var ifaces=os.networkInterfaces();
for (var dev in ifaces) {
var alias=0;
for (var index in ifaces[dev]) {
if (ifaces[dev][index].family=='IPv4') {
var myip = ifaces[dev][index].address;
console.log(dev+(alias?':'+alias:''), myip);
alias++;
}
}
}