-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
49 lines (38 loc) · 1.18 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
import express from 'express';
import React from 'react';
import ReactDOMServer from 'react-dom/server';
import { StaticRouter } from 'react-router';
import { Helmet } from 'react-helmet';
import { ServerStyleSheet } from 'styled-components';
import App from './src/app';
const app = express();
const PORT = 3000;
app.use(express.static('build/public'));
app.get('*', (req, res) => {
const sheet = new ServerStyleSheet();
const content = ReactDOMServer.renderToString(
sheet.collectStyles(
<StaticRouter location={req.url} context={{}}>
<App />
</StaticRouter>
)
);
const helmet = Helmet.renderStatic();
console.info('Rendered content', content);
res.send(`
<html>
<head>
${helmet.title.toString()}
${helmet.meta.toString()}
${sheet.getStyleTags()}
</head>
<body>
<div id="root">
${content}
</div>
<script src="/client_bundle.js"></script>
</body>
</html>
`)
});
app.listen(PORT, () => console.log('Server started...'));