-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
49 lines (41 loc) · 1.7 KB
/
index.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
const express = require('express');
const path = require('path');
const fs = require('fs');
const bodyParser = require('body-parser');
const csrf = require('csurf');
const cookieParser = require('cookie-parser');
const app = express();
const port = process.env.PORT || 3001;
const csrfProtection = csrf({ cookie: true });
app.use(bodyParser.json());
app.use(cookieParser());
app.post(
'/api/have_some_fun',
(req, res, next) => process.env.NODE_ENV === 'production' ? csrfProtection(req, res, next) : next(),
(req, res) => {
console.log(`request from: *** ${req.body.name} ***`);
res.status(200).json({ data: 'Here is your fun'});
}
);
// https://facebook.github.io/create-react-app/docs/title-and-meta-tags#generating-dynamic-meta-tags-on-the-server
// https://facebook.github.io/create-react-app/docs/title-and-meta-tags#injecting-data-from-the-server-into-the-page
const getRenderedIndexFile = (req, res) => {
const indexFile = path.join(__dirname, 'client/build', 'index.html');
fs.readFile(indexFile, 'utf8', (err, data) => {
if (data) {
res.send(
data.replace('{{__OG_TITLE__}}', 'Good luck, have fun.')
.replace('{{__SERVER_DATA__}}', 'Provided by Express')
.replace('{{__CSRF_TOKEN__}}', req.csrfToken())
);
}
});
};
if (process.env.NODE_ENV === "production") {
// This is to handle landing page (first render).
app.get('/', csrfProtection, getRenderedIndexFile);
app.use(express.static(path.join(__dirname, 'client/build')));
// This is to handle client-side routing (Avoid to get 404 page when refresh in other page).
app.get('*', csrfProtection, getRenderedIndexFile);
}
app.listen(port, () => console.log(`Listening on port ${port}`));