-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
46 lines (38 loc) · 1.26 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
import express from 'express';
import path from 'path';
import fetch from 'isomorphic-fetch';
import url from 'url';
import {
API_PROXY_FORECAST7_GET_URL,
FORECAST7_GET_URL,
} from './common/constants/urls';
const app = express();
const port = process.env.PORT || 5001;
app.listen(port, () => console.log(`Listening on port ${port}`));
app.use(express.static(__dirname + '/client/build'));
app.get('/splash', (req, res) => {
res.sendFile(path.join(__dirname + '/client/build/splash.html'));
});
app.get('/', (req, res) => {
res.sendFile(path.join(__dirname + '/client/build/index.html'));
});
// This is super dumb but due to some CORS issue we need to proxy this request
const fetchForecast7GetUrl = async (googlePlaceId) => {
let res = await fetch(`${FORECAST7_GET_URL}${googlePlaceId}`, {
method: 'GET',
headers: {
Accept: 'application/json',
Origin: 'https://weatherwidget.io',
Referer: 'https://weatherwidget.io/w/',
DNT: 1
}
})
res = await res.text();
return res
};
app.get(API_PROXY_FORECAST7_GET_URL, async (req, res) => {
const urlParts = url.parse(req.url, true);
const googlePlaceId = urlParts['query']['googlePlaceId'];
const forecast7Url = await fetchForecast7GetUrl(googlePlaceId)
res.send({forecast7Url});
});