I'm not referring to
127.0.0.1
But rather the one that other computers would use to access the machine e.g.
192.168.1.6
I'm not referring to
127.0.0.1
But rather the one that other computers would use to access the machine e.g.
192.168.1.6
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);
en0
, vnic0
, vnic01
. en0
is the one browsersync displays to me. –
Phlegm https://github.com/indutny/node-ip
var ip = require("ip");
console.dir ( ip.address() );
node-ip
–
Hydrate $ 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();
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;
.filter(x => x)
removes the undefined entries which I didn't see. I did test it, but I had no undefined entries. I tested it on a REPL server after that, though, and had undefined entries. Thanks, Ebrahim! –
Zena https://github.com/dominictarr/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.
savage one-liner incoming
based on accepted answer, this one will build an array of objects with conditional entries depending on address properties
[{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);
Explained :
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
)
], []);
Output example :
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"}
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.
req.socket.localAddress
will return the address of the machine node is running on based on the current connection.
If you have a public IP address of 1.2.3.4
and someone hits your node server from the outside then the value for req.socket.localAddress
will be "1.2.3.4"
.
If you hit the same server from localhost then the address will be "127.0.0.1"
If your server has multiple public addresses then the value of req.socket.localAddress
will be the correct address of the socket connection.
Modified a bit Ebrahim's answer with some es6 and module syntax, for leaner code:
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...'
© 2022 - 2024 — McMap. All rights reserved.