-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
36 lines (33 loc) · 879 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
35
36
const express = require('express')
const app = express()
const axios = require('axios')
const WEATHER_API = 'https://api.openweathermap.org/data/2.5/weather?appid=b1e678cd4eea353040a44e9eaaa70e61&units=metric'
const cache = {}
app.get('/', (req, res) => {
res.json({
text: 'hola'
})
})
app.get('/weather', async (req, res) => {
console.log('req', req.query)
if (cache[req.query.city]) {
console.log('HIT', req.query.city)
return res.json(cache[req.query.city])
}
try {
const { data } = await axios.post(`${WEATHER_API}&q=${req.query.city}`)
console.log('data', data)
cache[req.query.city] = data
res.json(data)
} catch (err) {
return {
err
}
}
})
app.get('/health', (req, res) => {
res.status(200).end()
})
app.listen(process.env.PORT || 3000, () => {
console.log('Server started on', process.env.PORT || 3000)
})