如何在 Node.js 中获取本地 IP 地址?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10750303/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I get the local IP address in Node.js?
提问by deltanovember
I'm not referring to
我不是指
127.0.0.1
127.0.0.1
But rather the one that other computers would use to access the machine e.g.
而是其他计算机用来访问机器的那个,例如
192.168.1.6
192.168.1.6
回答by seppo0010
http://nodejs.org/api/os.html#os_os_networkinterfaces
http://nodejs.org/api/os.html#os_os_networkinterfaces
var os = require('os');
var interfaces = os.networkInterfaces();
var addresses = [];
for (var k in interfaces) {
for (var k2 in interfaces[k]) {
var address = interfaces[k][k2];
if (address.family === 'IPv4' && !address.internal) {
addresses.push(address.address);
}
}
}
console.log(addresses);
回答by Jan J?na
https://github.com/indutny/node-ip
https://github.com/indutny/node-ip
var ip = require("ip");
console.dir ( ip.address() );
回答by Ebrahim Byagowi
My version which was needed for a compact and single file script, hope to be useful for others:
我的版本需要一个紧凑的单文件脚本,希望对其他人有用:
var ifs = require('os').networkInterfaces();
var result = Object.keys(ifs)
.map(x => [x, ifs[x].filter(x => x.family === 'IPv4')[0]])
.filter(x => x[1])
.map(x => x[1].address);
Or to answer the original question:
或者回答原来的问题:
var ifs = require('os').networkInterfaces();
var result = Object.keys(ifs)
.map(x => ifs[x].filter(x => x.family === 'IPv4' && !x.internal)[0])
.filter(x => x)[0].address;
回答by Alok G.
$ npm install --save quick-local-ip
follwed by
紧随其后
var myip = require('quick-local-ip');
//getting ip4 network address of local system
myip.getLocalIP4();
//getting ip6 network address of local system
myip.getLocalIP6();
回答by theram
https://github.com/dominictarr/my-local-ip
https://github.com/dominctarr/my-local-ip
$ npm install -g my-local-ip
$ my-local-ip
or
或者
$ npm install --save my-local-ip
$ node
> console.log(require('my-local-ip')())
A very small module that does just this.
一个非常小的模块,可以做到这一点。
回答by Intervalia
Ever since Node version 0.9.6 there is a simple way to get the server's IP address based on each request. This could be important if your machine has multiple IP addresses or even if you are doing something on localhost.
从 Node 0.9.6 版本开始,就有一种简单的方法可以根据每个请求获取服务器的 IP 地址。如果您的机器有多个 IP 地址,或者即使您在 localhost 上执行某些操作,这可能很重要。
req.socket.localAddresswill return the address of the machine node is running on based on the current connection.
req.socket.localAddress将返回基于当前连接运行的机器节点的地址。
If you have a public IP address of 1.2.3.4and someone hits your node server from the outside then the value for req.socket.localAddresswill be "1.2.3.4".
如果你有一个公网IP地址1.2.3.4,有人从外面打你的节点服务器,然后该值req.socket.localAddress会"1.2.3.4"。
If you hit the same server from localhost then the address will be "127.0.0.1"
如果您从 localhost 访问同一台服务器,则地址将是 "127.0.0.1"
If your server has multiple public addresses then the value of req.socket.localAddresswill be the correct address of the socket connection.
如果您的服务器有多个公共地址,那么 的值req.socket.localAddress将是套接字连接的正确地址。

