-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathserver.js
executable file
·80 lines (56 loc) · 1.74 KB
/
server.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
var express = require('express');
var geoip = require('geoip-lite');
var _ = require('lodash');
var countries = require('./data/countries-full');
var express_enforces_ssl = require('express-enforces-ssl');
var app = express();
var port = process.env.PORT || 5000;
app.enable('trust proxy');
app.use(express_enforces_ssl());
app.get('/country', function (req, res, next) {
res.json(countries);
})
app.get('/country/get', function (req, res, next) {
var country = _.find(countries, req.query);
if(country == undefined) {
res.json(204, { 'msg' : 'No country found'});
} else {
res.json(200, country);
}
})
app.get('/country/region', function (req, res, next) {
var result = [];
var country_region = _.find(countries, function(country) {
return
});
var country_region = _.reduce(countries, function(result, country, key) {
if(country.region.toLowerCase() == req.query.name.toLowerCase()) {
result.push(country);
}
return result;
}, []);
if(country_region == undefined) {
res.json(204, { 'msg' : 'No country found'});
} else {
res.json(200, country_region);
}
})
app.get('/', function (req, res, next) {
var ip = req.headers['x-forwarded-for']
|| req.connection.remoteAddress
|| req.socket.remoteAddress
|| req.connection.socket.remoteAddress;
var ip_lookup = geoip.lookup(ip);
if(ip_lookup !== null) {
var country_detail = _.find(countries, function(country) {
return country['cca2'] == ip_lookup.country
});
ip_lookup.country = country_detail;
}
var result = _.merge({ip : ip}, ip_lookup);
res.json(result);
});
app.listen(port, function() {
console.log("✔ Express server listening on port %d ", port);
});
module.exports = app;