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

如何在Node.js中获取本地IP地址?,node.js,Node.js,我不是指 127.0.0.1 而是其他计算机用来访问机器的那台 192.168.1.6 或 这是一个非常小的模块 $ 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 syste

我不是指

127.0.0.1

而是其他计算机用来访问机器的那台

192.168.1.6

这是一个非常小的模块

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

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

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);
或者回答原来的问题:

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;

自节点版本0.9.6以来,有一种简单的方法可以根据每个请求获取服务器的IP地址。如果您的计算机有多个IP地址,或者您正在本地主机上执行某些操作,这一点可能很重要

req.socket.localAddress
将返回基于当前连接运行的机器节点的地址

如果您的公共IP地址为
1.2.3.4
,并且有人从外部点击您的节点服务器,则
req.socket.localAddress
的值将为
“1.2.3.4”

如果从localhost点击同一服务器,则地址将为
“127.0.0.1”


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

savage one liner incoming

根据已接受的答案,这个函数将根据地址属性构建一个包含条件项的对象数组

[{name: {interface name}, ip: {ip address}}, ...]
解释:

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"}
使用一些es6和模块语法修改了一点,以实现更精简的代码:

从“操作系统”导入{networkInterfaces};
const netInterfaces=网络接口();
常量[{address}]=对象.值(netInterface).flatMap((netInterface)=>
netInterface.filter((prop)=>prop.family==“IPv4”和&!prop.internal)
);
console.log(地址)/->“192.168…”

对于那些搜索interwebz的人来说,这是一种更常见的网络IP。我采用了这种技术,并将其封装到一个名为接口地址的模块中。在这里查看它只是为了了解,为什么有不止一个IP地址?当我用gulp运行它时,我得到:
en0
vnic0
vnic01
en0
是browsersync向我显示的浏览器。您能帮我解决这个问题吗@seppo0010嘿,您可能想对如何过滤出有效的非内部ipv6地址发表评论。不过,这个问题的答案很好,谢谢。这是最短的解决方案。事实上,如果您只需要IP,这个解决方案比公认的解决方案长约380行,因为您需要包含
节点IP
。@wacko:那又怎样,它是测试版,提供了一个简洁的api。因为它在服务器上运行,所以在库中的loc实在不值得一提。而且它本身没有依赖性,这很好。这很酷,但在有专用网络设置的流浪者盒子中不起作用。当你真的想要eth1地址时,你会得到eth0地址。我一直在回ip 127.0.0.1而不是地址?那些looooong的1行程序快把我累死了。最好是多行缩进的。性感的解决方案。
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;
[{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"}