-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
30 lines (29 loc) · 1009 Bytes
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
(function () {
'use strict';
var http = require('http');
module.exports = {
endpoints: {
json: 'http://ip-api.com/json'
},
lookup: function (ip) {
return new Promise(
function (resolve, reject) {
var url = ip !== undefined && ip !== null && ip.length > 0 ?
this.endpoints.json + '/' + ip :
this.endpoints.json;
http.get(url, function (result) {
var response = '';
result.on('data', function (line) {
response += line;
});
result.on('end', function () {
resolve(JSON.parse(response));
});
}).on('error', function (error) {
reject(error);
});
}.bind(this)
);
}
};
}());