如何在Node.js中获取本地IP地址?

时间:2012-05-25 07:31:27

标签: node.js

我不是指

127.0.0.1

而是其他计算机用来访问机器的那个,例如

192.168.1.6

8 个答案:

答案 0 :(得分:111)

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);

答案 1 :(得分:97)

https://github.com/indutny/node-ip

var ip = require("ip");
console.dir ( ip.address() );

答案 2 :(得分:9)

我的版本是紧凑的单文件脚本所需要的,希望对其他人有用:

application.groovy

或回答原来的问题:

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);

答案 3 :(得分:8)

$ npm install --save quick-local-ip

后面的

var myip = require('quick-local-ip');

//getting ip4 network address of local system
myip.getLocalIP4();

//getting ip6 network address of local system
myip.getLocalIP6();

答案 4 :(得分:4)

https://github.com/dominictarr/my-local-ip

$ npm install -g my-local-ip
$ my-local-ip

$ npm install --save my-local-ip
$ node
> console.log(require('my-local-ip')())

这是一个非常小的模块。

答案 5 :(得分:0)

从Node 0.9.6版本开始,就有一种简单的方法可以根据每个请求获取服务器的IP地址。如果您的计算机具有多个IP地址,或者即使您正在localhost上执行某项操作,这也可能很重要。

req.socket.localAddress将根据当前连接返回正在运行的机器节点的地址。

如果您的公共IP地址为1.2.3.4,并且有人从外面打您的节点服务器,则req.socket.localAddress的值为"1.2.3.4"

如果您从本地主机访问同一服务器,则地址将为"127.0.0.1"

如果您的服务器有多个公共地址,则req.socket.localAddress的值将是套接字连接的正确地址。

答案 6 :(得分:0)

单线进货

根据公认的答案,这将根据地址属性构建一个带有条件条目的对象数组

[{name: {interface name}, ip: {ip address}}, ...]
const ips = Object.entries(require("os").networkInterfaces()).reduce((acc, iface) => [...acc, ...(iface[1].reduce((acc, address) => acc || (address.family === "IPv4" && !address.internal), false) ? [{name: iface[0], ip: iface[1].filter(address => address.family === "IPv4" && !address.internal).map(address => address.address)[0]}] : [])], []);
console.log(ips);

解释:

const ips = Object.entries(require("os").networkInterfaces()) // fetch network interfaces
.reduce((acc, iface) => [ // reduce to build output object
    ...acc, // accumulator
    ...(
        iface[1].reduce((acc, address) => acc || (address.family === "IPv4" && !address.internal), false) ? // conditional entry
        [ // validate, insert it in output
            { // create {name, ip} object
                name: iface[0], // interface name
                ip: iface[1] // interface IP
                .filter(address => address.family === "IPv4" && !address.internal) // check is IPv4 && not internal
                .map(address => address.address)[0] // get IP
            }
        ] 
        : 
        [] // ignore interface && ip
    )
], []);

输出示例:

Array(4) [Object, Object, Object, Object]
length:4
__proto__:Array(0) [, …]
0:Object {name: "vEthernet (WSL)", ip: "172.31.xxx.x"}
1:Object {name: "Ethernet", ip: "10.0.x.xx"}
2:Object {name: "VMware Network Adapter VMnet1", ip: "192.168.xxx.x"}
3:Object {name: "VMware Network Adapter VMnet8", ip: "192.168.xx.x"}

答案 7 :(得分:0)

使用一些es6和模块语法对Ebrahim's answer进行了一些修改,以获取更精简的代码:

<dependencies>
    <dependency>
        <groupId>org.json</groupId>
        <artifactId>json</artifactId>
        <version>20190722</version>
    </dependency>
    <dependency>
        <groupId>commons-io</groupId>
        <artifactId>commons-io</artifactId>
        <version>2.6</version>
    </dependency> 
</dependencies>