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 ?
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.
use externalip
package
https://github.com/alsotang/externalip
externalip(function (err, ip) {
console.log(ip); // => 8.8.8.8
});
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();
};
You can use nurl
library command ippublic
to get this. (disclosure: I made nurl)
> npm install nurl-cli -g
> ippublic;
// 50.240.33.6
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);
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.