-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
65 lines (50 loc) · 1.8 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
const express = require('express')
const requestify = require('requestify')
const app = express()
app.set('views', `${__dirname}/public`)
app.engine('html', require('ejs').renderFile)
app.use(express.static(`${__dirname}/public`))
const APIs = {
googleMaps: city => {
return `http://maps.googleapis.com/maps/api/geocode/json?address=${city}&sensor=false`
},
geoNames: (lat, lng) => {
const username = 'YOUR_USERNAME'
const host = 'http://ws.geonames.org'
const path = '/timezoneJSON?lat=' + lat + '&lng=' + lng + '&username=' + username
return host + path
}
}
app.get('/', (req, res) => {
res.render('timezone.html')
})
app.get('/getoffset/:city', function (req, res) {
res.setHeader('Content-Type', 'application/json')
const googleMapsURL = APIs.googleMaps(req.params.city)
requestify.get(googleMapsURL)
.then(function (response) {
var googleMapsData = response.getBody()
if (googleMapsData.status === 'OK') {
var firstRes = googleMapsData.results[0]
var coords = firstRes.geometry.location
var lat = coords.lat
var lng = coords.lng
var cityName = firstRes.address_components[0].long_name
var geoNamesURL = APIs.geoNames(lat, lng)
requestify.get(geoNamesURL)
.then(function (response) {
var geoNamesData = response.getBody()
var offset = geoNamesData.gmtOffset
res.send(JSON.stringify({ status: 'success', offset: offset, cityName: cityName }))
})
} else {
res.send(JSON.stringify({ status: 'cityNotFound' }))
};
}
)
})
var port = process.env.PORT || 5000
// Listen to server
app.listen(port, function () {
console.log('Express server listening on %d, in %s mode', port, app.get('env'))
})