-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstats.js
55 lines (49 loc) · 1.26 KB
/
stats.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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
/* */
const path = require('path');
const http = require("http");
const lib = require(path.join(__dirname, '_lib/lib.js'));
function updateStats(req, res, c, next) {
const test = lib.getIP(req);
if (test) {
if (c.app.mode === 'production') {
console.log(test + " " + req.originalUrl);
sendHit("geoip");
} else {
console.log('SAVE TEST ...');
}
} else {
console.log(test, 'DONT SAVE => ');
}
next();
}
function sendHit(service) {
let path = "http://localhost:3970/addhit/" + service;
makeHttpRequest(path, function (err, res, data) {
if (err) {
console.error('Error with the request:', err.message);
}
});
}
function makeHttpRequest(path, callback) {
http.get(path, function (resp) {
let data = '';
resp.on('data', function (chunk) {
data += chunk;
});
resp.on('end', function () {
try {
var parsed = JSON.parse(data);
} catch (err) {
//console.error('Unable to parse response as JSON', err);
return callback(err, null, null);
}
callback(null, resp, parsed);
});
}).on('error', (err) => {
//console.error('Error with the request:', err.message);
callback(err, null, null);
});
}
module.exports = {
updateStats: updateStats,
};