-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
53 lines (42 loc) · 1.28 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
'use strict';
require('dotenv').config();
const express = require('express');
const bodyParser = require('body-parser');
const fs = require('fs');
/* courses.json automatically created with "npm run update" */
const courses = require('./courses.json');
const app = express();
app.use(bodyParser.json());
if (process.env.NODE_ENV === 'production') {
app.use(express.static(`${__dirname}/client/build`));
}
const PORT = process.env.PORT || 5000;
app.listen(PORT, () =>
console.log(`listening on ${PORT} in ${process.env.NODE_ENV} mode`),
);
// force HTTPS redirect
if (process.env.NODE_ENV === 'production') {
app.all('*', (req, res, next) => {
if (req.headers['x-forwarded-proto'] !== 'https') {
res.redirect(`https://${req.get('host')}${req.url}`);
} else {
next();
}
});
}
app.get('/test', async (req, res) => {
res.json({ message: 'API works whoopee :DD' });
});
app.get('/course', async (req, res) => {
const id = req.query.id;
const course = courses.find(a => a.id == id);
if (course) {
res.json(course);
} else {
res.json({ error: `No course found with id ${id}` });
}
});
// Respond to all requests with index.html and let React Router do the routing.
app.get('*', (req, res) => {
res.sendFile(`${__dirname}/client/build/index.html`);
});