-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
44 lines (37 loc) · 1.03 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
import "core-js/stable";
import express from "express";
import compression from "compression";
import React from "react";
import ReactDOMServer from "react-dom/server";
import { StaticRouter } from "react-router";
const app = express();
const PORT = process.env.PORT || 3000;
app.use(compression());
app.use(express.json());
app.use(express.static("build/public", { index: false }));
import App from "./src/app";
app.get("*", (req, res) => {
const context = {};
const content = ReactDOMServer.renderToString(
<StaticRouter location={req.url} context={context}>
<App />
</StaticRouter>
);
const html = `
<html>
<head>
<title>test</title>
</head>
<body>
<div id="root">
${content}
</div>
<script src="client_bundle.js"></script>
</body>
</html>
`;
res.send(html);
});
app.listen(PORT, () => {
console.log(`listening on port: ${PORT}`);
});