UNPKG

getip2location

Version:

Find IP to Location. Get Location against IP Address of Users.

34 lines (24 loc) 826 B
var http=require('http'); var exports=module.exports = {}; exports.getip2location=function(ip, cback) { //OPTIONS var options = { protocol:'http:', host: 'ipinfo.io', port:80, path: '/'+ip+'/json', method:'GET' }; //CALLBACK var callback = function(response) { var str = ''; //another chunk of data has been recieved, so append it to `str` response.on('data', function (chunk) { str += chunk; }); //the whole response has been recieved, so we just print it out here response.on('end', function () { cback(str); }); } var request=http.request(options, callback); request.on('error', function(err) { // handle errors with the request itself console.error('Error with the request:', err.message); }); request.end(); }