-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
62 lines (51 loc) · 1.41 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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
const express = require('express');
const request = require('request');
const compression = require('compression')
const isDev = process.env.NODE_ENV !== 'production';
const dotenv = isDev ? require('dotenv') : false;
if (dotenv) {
dotenv.load();
}
const app = express();
app.set('port', process.env.PORT || 3001);
app.use(compression());
// Express only serves static assets in production
if (process.env.NODE_ENV === 'production') {
app.use(express.static('client/build'));
}
app.get('/api/locations', (req, res) => {
const address = req.query.address;
if (!address) {
res.json({
error: 'Missing required parameter `address`',
});
return;
}
req
.pipe(
request(
`https://maps.googleapis.com/maps/api/geocode/json?address=${address}&key=${process.env.GOOGLE_MAPS_API_KEY}`
)
)
.pipe(res);
});
app.get('/api/forecasts', (req, res) => {
const latitude = req.query.latitude;
const longitude = req.query.longitude;
if (!latitude || !longitude) {
res.json({
error: 'Missing required parameter `latitude` or `longitude`',
});
return;
}
req
.pipe(
request(
`https://api.darksky.net/forecast/${process.env.DARK_SKY_API_KEY}/${latitude},${longitude}`
)
)
.pipe(res);
});
app.listen(app.get('port'), () => {
console.log(`Find the server at: http://localhost:${app.get('port')}/`); // eslint-disable-line no-console
});