Skip to content

Instantly share code, notes, and snippets.

@ry
Created April 3, 2011 10:14
Show Gist options
  • Save ry/900338 to your computer and use it in GitHub Desktop.
Save ry/900338 to your computer and use it in GitHub Desktop.
var http = require('http');
exports.query = function(cb) {
var cbCalled = false;
http.get({ host: 'jsonip.com' }, function(res) {
var buffer = '';
res.setEncoding('utf8');
res.on('data', function(d) {
buffer += d;
});
res.on('end', function() {
if (!cbCalled) {
var i;
try {
i = JSON.parse(buffer);
} catch (e) {
cbCalled = true;
cb(e);
return;
}
cbCalled = true;
cb(null, i.ip);
}
});
}).on('error', function(err) {
if (!cbCalled) {
cbCalled = true;
cb(err);
}
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment