-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
51 lines (42 loc) · 990 Bytes
/
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
var url = require('url')
var http = require('http')
var middleware = require('./lib/json-response-middleware')
var geoIpQuery = require('./lib/geo-ip-query')
const port = process.env.PORT || 3000
http.createServer((req, res) => {
middleware(res)
var parsedUrl = url.parse(req.url, true);
if (parsedUrl.pathname != '/') {
res.jsonNotFound()
return
}
var ip = parsedUrl.query.ip
if (!ip) {
res.jsonError({
type: 'ip_required'
})
return
}
geoIpQuery(ip).then(g => {
if (!g){
res.jsonError({
type: 'ip_not_found'
}, {
ip: ip
})
return
}
res.jsonOk({
ip: ip,
geo: g
})
})
.catch(e => {
res.jsonError({
type: 'invalid_ip'
}, {
ip: ip
});
})
}).listen(port)
//console.log(`Server listening on port ${port}`)