老馮 發表於 2020-5-25 18:28:00

Node js获取本地ip地址

<p>使用webpack 和 vue进行本地开发的时候,可能会遇到让同在局域网的同事访问。。。我们在config/index.js文件中配置的dev环境一般情况如下:</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 0, 1)">proxyTable: {
   </span>'/apis'<span style="color: rgba(0, 0, 0, 1)">: {
      target: `http:</span><span style="color: rgba(0, 128, 0, 1)">//公网ip</span><span style="color: rgba(0, 128, 0, 1)">`,</span>
      changeOrigin: <span style="color: rgba(0, 0, 255, 1)">true</span><span style="color: rgba(0, 0, 0, 1)">,
      </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> secure: false,//target默认情况下,不接受运行在HTTPS上,且使用了无效证书的后端服务器。如果你想要接受, 则需设置该项为false</span>
<span style="color: rgba(0, 0, 0, 1)">      pathRewrite: {
          </span>'^/apis': ''<span style="color: rgba(0, 0, 0, 1)">
      },
      ws: </span><span style="color: rgba(0, 0, 255, 1)">true</span><span style="color: rgba(0, 0, 0, 1)">
    },
},
host: </span>'0.0.0.0',<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> can be overwritten by process.env.HOST<br><br></span>port: 80, // can be overwritten by process.env.PORT, if port is in use, a free one will be determined</pre>
</div>
<p>我们前端访问页面就是 http://localhost ,我们访问后端接口是用 localhost域名访问的(webpack会给我们代理转发请求,不会出现跨域)。</p>
<p>如果同事访问你的页面,只能通过你的局域网ip地址进行访问, 比如你的地址是&nbsp;172.16.79.192,你的同事访问这个地址,页面访问正常,但是访问接口回报跨域(172.16.79.192和localhost).</p>
<p>这时候你可以改config/index.js文件中的host为你的ip,然后在改访问后端接口为你的ip,重新启动项目,然后你的同事才可以正常访问和使用。。。</p>
<p>我们仔细想一下,如果在启动的时候,将上面的解决方案搞定,可不可行呢?当然可以。。。我们可以在项目启动前,用在node获取你的电脑的局域网地址,分别赋值即可,下面贴一段node获取本地ip地址的代码:</p>
<div class="cnblogs_code">
<pre>const os = require('os'<span style="color: rgba(0, 0, 0, 1)">);

</span><span style="color: rgba(0, 128, 0, 1)">/*</span><span style="color: rgba(0, 128, 0, 1)">*
* 获取当前机器的ip地址
</span><span style="color: rgba(0, 128, 0, 1)">*/</span>
<span style="color: rgba(0, 0, 255, 1)">function</span><span style="color: rgba(0, 0, 0, 1)"> getIpAddress() {
</span><span style="color: rgba(0, 0, 255, 1)">var</span> ifaces=<span style="color: rgba(0, 0, 0, 1)">os.networkInterfaces()

</span><span style="color: rgba(0, 0, 255, 1)">for</span> (<span style="color: rgba(0, 0, 255, 1)">var</span> dev <span style="color: rgba(0, 0, 255, 1)">in</span><span style="color: rgba(0, 0, 0, 1)"> ifaces) {
    let iface </span>=<span style="color: rgba(0, 0, 0, 1)"> ifaces

    </span><span style="color: rgba(0, 0, 255, 1)">for</span> (let i = 0; i &lt; iface.length; i++<span style="color: rgba(0, 0, 0, 1)">) {
      let {family, address, internal} </span>=<span style="color: rgba(0, 0, 0, 1)"> iface

      </span><span style="color: rgba(0, 0, 255, 1)">if</span> (family === 'IPv4' &amp;&amp; address !== '127.0.0.1' &amp;&amp; !<span style="color: rgba(0, 0, 0, 1)">internal) {
      </span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> address
      }
    }
}
}

let ipAddress </span>=<span style="color: rgba(0, 0, 0, 1)"> getIpAddress()
console.log(ipAddress)</span></pre>
</div>
<p>最后附上我测试的截图:</p>
<p><img src="https://img2020.cnblogs.com/blog/893018/202005/893018-20200525182552775-711949781.png" alt=""></p>
<p>&nbsp;</p>
<h2>&nbsp;参考资料:</h2>
<p>  http://nodejs.cn/api/os.html#os_os_networkinterfaces</p>
<p>&nbsp;</p><br><br>
来源:https://www.cnblogs.com/hanshuai/p/12959979.html
頁: [1]
查看完整版本: Node js获取本地ip地址