介绍
这篇文章将为大家详细讲解有关使用节点。js怎么获取本机IP地址,文章内容质量较高,因此小编分享给大家做个参考,希望大家阅读完这篇文章后对相关知识有一定的了解。
<强>——web <强>客户端JavaScript 强> 强>
& lt; !——,调用方式,——祝辞, & lt; body  onload=癱heckCookie()“祝辞& lt;/body>, function getYourIP () { const RTCPeerConnection =, window.RTCPeerConnection | |, window.webkitRTCPeerConnection | |, window.mozRTCPeerConnection; if (RTCPeerConnection),(()函数 { const rtc =, new RTCPeerConnection ({iceServers: []}); if (1, | |, window.mozRTCPeerConnection) { rtc.createDataChannel(& # 39; & # 39;,,{可靠:假}); } 时间=rtc.onicecandidate 函数(evt) { if (evt.candidate), grepSDP (“=$ {evt.candidate.candidate}”); }; rtc.createOffer(函数(offerDesc) { grepSDP (offerDesc.sdp); rtc.setLocalDescription (offerDesc); },,函数(e), {console.warn (& # 39; offer 失败# 39;,,e);}); const addrs =, Object.create(空); addr[& # 39; 0.0.0.0 # 39;],=,假; function updateDisplay (newAddr) { if (newAddr addr拷贝),返回; addr [newAddr],=,真的; const displayAddrs =,种(addr) .filter(函数(k), {return addr [k];}); for (let 小姐:=,0;,小姐:& lt;, displayAddrs.length;,我+ +) { if (displayAddrs[我].length 祝辞,16) { displayAddrs.splice(我,,1); 我,; } } console.info(& # 39;您的IP:, & # 39;,, displayAddrs [0]); } function grepSDP (sdp) { sdp.split (& # 39; \ r \ n # 39;) .forEach(函数(指数,还以为;arr) { if (~ line.indexOf(& # 39;=候选人# 39;)) { const parts =, line.split (& # 39;, & # 39;), 时间=addr 部分[4], 时间=type 部分[7]; if (type ===, & # 39;主机# 39;),updateDisplay (addr); } else if (~ line.indexOf (& # 39; c=& # 39;)) { const parts =, line.split (& # 39;, & # 39;), 时间=addr 部分[2]; updateDisplay (addr); } }); } }) (); 其他的 { console.warn(& # 39;请使用主流浏览器:chrome, firefox、opera, safari # 39;); } }
<>强——web <强>服务端强>节点。js 强>
const os =,要求(& # 39;操作系统# 39;); , module.exports ={ getLocalIP :,函数() { const eth0 =, os.networkInterfaces () .eth0; let localhost =,空; for (let 小姐:=,0;,小姐:& lt;, eth0.length;,我+ +) { if (eth0[我].family ==, & # 39; ipv4 # 39;) { 时间=localhost eth0[我].address; } } return 本地主机; }, };
关于使用node . js怎么获取本机IP地址就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看的到。