-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
30 lines (23 loc) · 947 Bytes
/
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
const port = 8000;
const express = require('express');
const axios = require('axios');
const cors = require('cors');
const path = require('path')
require('dotenv').config({ path: '.env' });
const app = express();
app.use(cors());
if (process.env.NODE_ENV === 'production') {
app.use(express.static('build'));
app.get('/', (req, res) => {
req.sendFile(path.resolve(__dirname, 'build', 'index.html'));
})
};
app.get('/getWeather', (req, res) => {
const BASE_URL = 'https://api.openweathermap.org/data/2.5';
const url = new URL(BASE_URL + '/' + req.query[1].serviceType);
url.search = new URLSearchParams({ ...req.query[0], appid: process.env.API_KEY });
axios.get(url)
.then(response => res.status(200).json(response.data))
.catch(error => console.log(error))
});
app.listen(process.env.PORT || port, () => console.log(`Server is running on port ${process.env.PORT || port}`));