Get local IP address in node.js

2019-01-03 04:02发布

I have a simple node.js program running on my machine and I want to get local IP address of PC on which is my program running. How do I get it with node.js?

30条回答
该账号已被封号
2楼-- · 2019-01-03 04:38

Install a module called ip like

npm install ip

then use this code.

var ip = require("ip");
console.log( ip.address() );
查看更多
欢心
3楼-- · 2019-01-03 04:38

Calling ifconfig is very platform-dependent, and the networking layer does know what ip addresses a socket is on, so best is to ask it. Node doesn't expose a direct method of doing this, but you can open any socket, and ask what local IP address is in use. For example, opening a socket to www.google.com:

var net = require('net');
function getNetworkIP(callback) {
  var socket = net.createConnection(80, 'www.google.com');
  socket.on('connect', function() {
    callback(undefined, socket.address().address);
    socket.end();
  });
  socket.on('error', function(e) {
    callback(e, 'error');
  });
}

Usage case:

getNetworkIP(function (error, ip) {
    console.log(ip);
    if (error) {
        console.log('error:', error);
    }
});
查看更多
ら.Afraid
4楼-- · 2019-01-03 04:38

I realise this is an old thread, but I'd like to offer an improvement on the top answer for the following reasons:

  • Code should be as self explanatory as possible.
  • Enumerating over an array using for...in... should be avoided.
  • for...in... enumeration should be validated to ensure the object's being enumerated over contains the property you're looking for. As javsacript is loosely typed and the for...in... can be handed any arbitory object to handle; it's safer to validate the property we're looking for is available.

    var os = require('os'),
        interfaces = os.networkInterfaces(),
        address,
        addresses = [],
        i,
        l,
        interfaceId,
        interfaceArray;
    
    for (interfaceId in interfaces) {
        if (interfaces.hasOwnProperty(interfaceId)) {
            interfaceArray = interfaces[interfaceId];
            l = interfaceArray.length;
    
            for (i = 0; i < l; i += 1) {
    
                address = interfaceArray[i];
    
                if (address.family === 'IPv4' && !address.internal) {
                    addresses.push(address.address);
                }
            }
        }
    }
    
    console.log(addresses);
    
查看更多
别忘想泡老子
5楼-- · 2019-01-03 04:39

use npm ip module

var ip = require('ip');

console.log(ip.address());

> '192.168.0.117'
查看更多
ら.Afraid
6楼-- · 2019-01-03 04:39

I'm using node.js 0.6.5

$ node -v
v0.6.5

Here is what I do

var util = require('util');
var exec = require('child_process').exec;
function puts(error, stdout, stderr) {
        util.puts(stdout);
}
exec("hostname -i", puts);
查看更多
▲ chillily
7楼-- · 2019-01-03 04:42

Any IP of your machine you can find by using the os module - and that's native to NodeJS

var os = require( 'os' );

var networkInterfaces = os.networkInterfaces( );

console.log( networkInterfaces );

All you need to do is call os.networkInterfaces() and you'll get an easy manageable list - easier than running ifconfig by leagues

http://nodejs.org/api/os.html#os_os_networkinterfaces

Best

Edoardo

查看更多
登录 后发表回答