Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I get the local IP address in Node.js?

Tags:

node.js

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

like image 977
deltanovember Avatar asked May 25 '12 07:05

deltanovember


People also ask

How do I find local IP?

First, click on your Start Menu and type cmd in the search box and press enter. A black and white window will open where you will type ipconfig /all and press enter. There is a space between the command ipconfig and the switch of /all. Your ip address will be the IPv4 address.

Can I get IP address from JavaScript?

To get the client's public IP address, JavaScript acts as a third-party server-side language. JavaScript can't do it alone, so, jQuery is added to do this. JavaScript works with third-party applications to fetch the IP addresses.

What is a node IP address?

An IP Address node represents an IPv4 or IPv6 address of a host or a device.


2 Answers

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); 
like image 160
seppo0010 Avatar answered Sep 17 '22 19:09

seppo0010


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

var ip = require("ip"); console.dir ( ip.address() ); 
like image 38
Jan Jůna Avatar answered Sep 21 '22 19:09

Jan Jůna