-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
67 lines (56 loc) · 1.8 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
63
64
65
66
67
// Import dependencies
const express = require('express');
const cors = require('cors');
const axios = require('axios');
const path = require('path');
const ORDERS_URL =
'https://gist.githubusercontent.com/ryanjn/07512cb1c008a5ec754aea6cbbf4afab/raw/eabb4d324270cf0d3d17a79ffb00ff3cfaf9acc3/orders.json';
// Create a new express application named 'app'
const app = express();
// Set our backend port to be either an environment variable or port 5000
const port = process.env.PORT || 5000;
// This application level middleware prints incoming requests to the servers console, useful to see incoming requests
app.use((req, res, next) => {
console.log(`Request_Endpoint: ${req.method} ${req.url}`);
next();
});
// Configure the bodyParser middleware
app.use(express.json());
// Configure the CORs middleware
app.use(cors());
// This is an endpoint to get orders
app.get('/api/orders', async (req, res) => {
try {
const { data: orders } = await axios.get(ORDERS_URL);
if (orders) {
res.status(200).json({
status: 'success',
results: orders.length,
data: {
orders
}
});
}
} catch (error) {
res.status(400).json({
status: 'fail',
message: 'Something went wrong'
});
}
});
// This middleware informs the express application to serve our compiled React files
if (process.env.NODE_ENV === 'production' || process.env.NODE_ENV === 'staging') {
app.use(express.static(path.join(__dirname, 'client/build')));
app.get('*', function(req, res) {
res.sendFile(path.join(__dirname, 'client/build', 'index.html'));
});
}
// Catch any bad requests
app.get('*', (req, res) => {
res.status(400).json({
status: 'fail',
msg: 'Bad Request'
});
});
// Configure our server to listen on the port defiend by our port variable
app.listen(port, () => console.log(`BACK_END_SERVICE_PORT: ${port}`));