-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
34 lines (31 loc) · 1018 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
31
32
33
34
var express = require('express')
var app = express()
var fetch = require('node-fetch')
const cityId = 498817
const port = 3000
const kelvinConst = 273.15
app.get('/:id', (req, res) => {
const url = `https://api.openweathermap.org/data/2.5/forecast?id=${req.params.id || cityId}&APPID=2a2062bcc18e7a3c8fec865a4b392092`
const options = {
method: 'GET',
headers: { 'Content-Type': 'application/json' }
}
return fetch(url, options)
.then(response => response.json())
.then(response => {
const data = response.list
const dataByDays = data.reduce((sum, item) => {
const min = item.main.temp_min
const max = item.main.temp_max
sum[new Date(item.dt * 1000)] = `${(min - kelvinConst).toPrecision(3)}°C - ${(max - kelvinConst).toPrecision(3)}°C`
return sum
}, {
id: +(new Date())
})
res.send(dataByDays)
})
.catch(err => res.json(err))
})
app.listen(port, _ => {
console.log('Example app listening at port: ', port)
})