63

我不是指

127.0.0.1

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

192.168.1.6

4

8 回答 8

120

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);
于 2012-05-25T14:42:34.320 回答
102

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

var ip = require("ip");
console.dir ( ip.address() );
于 2013-12-26T15:25:35.210 回答
10

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

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;
于 2016-08-13T04:31:58.490 回答
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();
于 2015-09-27T04:40:16.487 回答
5

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')())

一个非常小的模块就是这样做的。

于 2015-06-16T09:57:52.157 回答
1

野蛮单线来袭

根据接受的答案,这将根据地址属性构建具有条件条目的对象数组

[{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"}
于 2020-06-17T13:42:25.443 回答
0

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

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

如果您的公共 IP 地址为1.2.3.4并且有人从外部访问您的节点服务器,那么 的req.socket.localAddress值为"1.2.3.4"

如果您从 localhost 访问同一服务器,则地址将是"127.0.0.1"

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

于 2019-06-06T18:00:22.473 回答
0

用一些 es6 和模块语法修改了一些Ebrahim 的答案,以获得更精简的代码:

import { networkInterfaces } from "os";
const netInterfaces = networkInterfaces();
const [{ address }] = Object.values(netInterfaces).flatMap((netInterface) =>
  netInterface.filter((prop) => prop.family === "IPv4" && !prop.internal)
);
console.log(address) // -> '192.168...'
于 2020-12-15T11:26:32.827 回答