diff --git a/js/geoip/gps5.js b/js/geoip/gps5.js new file mode 100644 index 0000000..82935f2 --- /dev/null +++ b/js/geoip/gps5.js @@ -0,0 +1,37 @@ +/* GPS location based on an external database lookup */ + +/* requires https */ + +var serviceHost="api.beacondb.net", + servicePath="/v1/geolocate?key=test"; + + + +function geolocate (cb) { + var https; + if (typeof require == 'function') try { + https = require('https'); + } catch (e) { /* TODO Browser */ } + if (!https) return cb(new Error('ENETWORK')); + var req = https.request({ + hostname: serviceHost, + port: 443, + path: servicePath, + method: 'GET' + }, function (res) { + res.on('data', function (d){ + try { + var json = JSON.parse(d); + cb(json) + } catch (e) { cb(e) }; + }); + }) + + req.on('error', function (e) { + console.error(e); + cb(e); + }); + req.end(); +} + +module.exports = { geolocate : geolocate };