-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathapp.js
51 lines (40 loc) · 1.44 KB
/
app.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
require('./tracing.js');
const express = require('express');
const got = require('got');
const slshttp = require('serverless-http')
const WEATHER_API_URL = 'https://weather.workshop.epsagon.com/weather';
const NEWS_API_URL = 'https://news.workshop.epsagon.com/news';
const FACT_API_URL = 'https://facts.workshop.epsagon.com/facts';
function getWeather(city = '') {
const URL = `${WEATHER_API_URL}/${city}`
return got(URL).json().catch((err) => null); // json
}
function getNews(city = '') {
const URL = `${NEWS_API_URL}/${city}`
return got(URL).json().catch((err) => null); // json
}
function getFactForToday() {
const d = new Date();
const month = d.getMonth() + 1;
const day = d.getDate();
const URL = `${FACT_API_URL}/${month}/${day}`;
return got(URL).text().catch((err) => null); // string
}
const app = express();
app.get('/digest/:city', async (req, res) => {
const city = req.params.city;
const [weather, news, fact] = await Promise.all([
getWeather(city),
getNews(city),
getFactForToday()
]);
res.json({ weather, news, fact });
});
app.get('/proxy/:city', async(req, res) => {
res.json(await got(`http://localhost:3000/digest/${req.params.city}`).json().catch((err) => null))
})
app.use('*', (req, res) => {
res.status(404).send('Not Found');
})
app.listen(3000, () => console.log('App is now online at port 3000'));
//module.exports.handler = slshttp(app)