fonini fonini - 5 months ago 36
Node.js Question

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

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 ?

Answer

http://fugal.net/ip.cgi was similar to that one.

or you can

require('http').request({
    hostname: 'fugal.net',
    path: '/ip.cgi',
    agent: false
    }, function(res) {
    if(res.statusCode != 200) {
        throw new Error('non-OK status: ' + res.statusCode);
    }
    res.setEncoding('utf-8');
    var ipAddress = '';
    res.on('data', function(chunk) { ipAddress += chunk; });
    res.on('end', function() {
        // ipAddress contains the external IP address
    });
    }).on('error', function(err) {
    throw err;
}).end();

Ref: http://www.nodejs.org/api/http.html#http_http_request_options_callback

Comments