-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathserver.js
52 lines (42 loc) · 1.26 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
require('babel-register')({
presets: ['env', 'react', 'stage-2', 'es2015', 'stage-0'],
});
const STATIC_VERSION = (new Date()).getTime();
const path = require('path');
const ejs = require('ejs');
const express = require('express');
const renderStatic = require('./src/renderStatic').default;
const routes = require('./src/routes').default;
const { createStore } = require('./src/store');
const app = express();
app.disable('x-powered-by');
app.use(express.static('public'));
routes.forEach((route) => {
app.get(route.path, async (req, res) => {
const store = createStore();
let contentMetaTags;
if (typeof route.getData === 'function') {
try {
const data = await route.getData(store, req.params);
if (data && data.contentMetaTags) {
({ contentMetaTags } = data);
}
} catch (e) {
console.error(e);
}
}
const templateData = {
contentMetaTags,
state: store.getState(),
content: renderStatic(store, req.url),
staticVersion: STATIC_VERSION,
};
try {
const html = await ejs.renderFile(path.resolve(__dirname, 'src/index.ejs'), templateData);
res.send(html);
} catch (e) {
res.status(500).send(e);
}
});
});
app.listen(process.env.PORT || 3000);