generated from wyncode/midterm_project_template
-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.js
37 lines (35 loc) · 1.09 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
if (process.env.NODE_ENV !== 'production') {
require('dotenv').config();
}
const express = require('express');
const path = require('path');
const app = express();
const axios = require('axios');
app.get('/api/search', async (request, response) => {
const { search } = request.query;
try {
if (search) {
console.log('hello');
console.log(search);
const { data } = await axios.get(
`https://api.openweathermap.org/data/2.5/weather?q=${search}&units=imperial&appid=${process.env.API_KEY}`
);
response.json(data);
}
} catch (err) {
console.log(err.toString());
response.json({ error: err.toString() });
}
});
if (process.env.NODE_ENV === 'production') {
// Serve any static files
app.use(express.static(path.join(__dirname, 'client/build')));
// Handle React routing, return all requests to React app
app.get('*', (request, response) => {
response.sendFile(path.join(__dirname, 'client/build', 'index.html'));
});
}
const port = process.env.PORT || 8080;
app.listen(port, () => {
console.log(`API listening on port ${port}...`);
});