-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
50 lines (42 loc) · 1.18 KB
/
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
const express = require('express');
const _ = require('lodash');
const axios = require('axios');
const memoize = require('memoizee');
const app = express();
const port = 3000;
app.get('/', async (req, res, next) => {
try{
const city = req.query.city;
if (!city){
res.status(400)
res.send({
msg: "Must supply city as a parameter",
});
next(new Error("Must supply city as a parameter"))
}
const response = await getWeatherData(city);
return res.send({
...response
});
} catch (e){
next(e);
}
});
const getWeatherData = memoize(async (city) => {
const apiKey = "89e930a8066c712e54f311a0c03fe339";
console.log("Fetching data!");
try {
const response = await axios.get('http://api.openweathermap.org/data/2.5/weather', {
params:{
apiKey: apiKey,
q: city,
units: 'metric'
}});
return response.data;
} catch (e){
console.log(e);
throw e;
}
}, {maxAge: 10000});
// app.listen(port, () => console.log(`Example app listening on port ${port}!`));
module.exports = app