How to get my external IP address with node.js?

后端 未结 14 1505
情深已故
情深已故 2020-12-06 05:00

I\'m using node.js and I need to get my external IP address, provided by my ISP. Is there a way to achieve this without using a service like http://myexternalip.com/raw ?

相关标签:
14条回答
  • 2020-12-06 05:26

    My shameless plug: canihazip (Disclosure: I'm the author of module, but not of the main page.)

    It can be required as a module, exposing a single function that can optionally be passed a callback function an it will return a promise.

    It can be also be installed globally and used as CLI.

    0 讨论(0)
  • 2020-12-06 05:31

    use externalip package

    https://github.com/alsotang/externalip

    externalip(function (err, ip) {
      console.log(ip); // => 8.8.8.8
    });
    
    0 讨论(0)
  • 2020-12-06 05:31

    You may use the request-ip package:

    const requestIp = require('request-ip');
    
    // inside middleware handler
    const ipMiddleware = function(req, res, next) {
        const clientIp = requestIp.getClientIp(req); 
        next();
    };
    
    0 讨论(0)
  • 2020-12-06 05:34

    You can use nurl library command ippublic to get this. (disclosure: I made nurl)

    > npm install nurl-cli -g
    > ippublic;
    // 50.240.33.6
    
    0 讨论(0)
  • 2020-12-06 05:36

    npm install --save public-ip from here.

    Then

    publicIp.v4().then(ip => {
      console.log("your public ip address", ip);
    });
    

    And if you want the local machine ip you can use this.

    var ip = require("ip");
    var a = ip.address();
    console.log("private ip address", a);
    
    0 讨论(0)
  • 2020-12-06 05:36

    I was looking for a solution not relying to other's libraries/ resources, and found this as acceptable alternative:

    Just a GET request to external server ( under my control ), where I read req.headers['x-forwarded-for'] and serve it back to my client.

    0 讨论(0)
提交回复
热议问题