-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
29 lines (25 loc) · 920 Bytes
/
Copy pathserver.js
File metadata and controls
29 lines (25 loc) · 920 Bytes
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
const express = require('express')
const request = require('request')
const path = require('path');
const app = express();
const port = process.env.PORT || 5000;
app.get('/api/joke', (req, res) => {
//Make the API call to get the random joke
request('https://icanhazdadjoke.com/', { json: true }, (error, response, body) => {
if (!error && response.statusCode == 200) {
console.log(body);
res.json(body);
} else {
return console.log(error);
}
});
});
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('*', function (req, res) {
res.sendFile(path.join(__dirname, 'client/build', 'index.html'));
});
}
app.listen(port, () => console.log(`Listening on port ${port}`));