This repository was archived by the owner on Jan 11, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
57 lines (50 loc) · 1.86 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
const axios = require('axios');
const express = require('express');
const path = require('path');
const app = express();
if (process.env.NODE_ENV !== 'DEVELOPMENT') {
require('dotenv').config();
}
app.get(`/drinks/:item`, (request, response) => {
axios
.get(`https://www.thecocktaildb.com/api/json/v2/${process.env.API_KEY}/filter.php?i=${request.params.item}`)
.then(cocktailResponse => {
console.log(request.params.item);
console.log('cocktail response', cocktailResponse);
response.json(cocktailResponse.data || []);
})
.catch(e => console.error(e));
});
app.get('/random-drink', (request, response) => {
axios
.get(`https://www.thecocktaildb.com/api/json/v2/${process.env.API_KEY}/random.php`)
.then(cocktailResponse => response.json(cocktailResponse.data))
.catch(e => console.error(e));
});
app.get('/pages/Drink/:id', (request, response) => {
axios
.get(`https://www.thecocktaildb.com/api/json/v1/1/lookup.php?i=${request.params.id}`)
.then(cocktailResponse => response.json(cocktailResponse.data))
.catch(error => console.log(error));
});
if (process.env.NODE_ENV === 'DEVELOPMENT') {
// 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'));
});
}
if (process.env.NODE_ENV === 'production') {
// Exprees will serve up production assets
app.use(express.static('client/build'));
// Express serve up index.html file if it doesn't recognize route
const path = require('path');
app.get('*', (req, res) => {
res.sendFile(path.resolve(__dirname, 'client', 'build', 'index.html'));
});
}
const port = process.env.PORT || 8080;
app.listen(port, () => {
console.log(`API listening on port ${port}...`);
});