-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.js
86 lines (73 loc) · 2.33 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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
import express from "express";
import React from "react";
import "isomorphic-fetch";
import path from "path";
import fs from "fs";
import { StaticRouter } from "react-router-dom/server";
import { renderToString } from "react-dom/server";
import App from "./src/App";
import { ServerStyleSheet } from "styled-components";
import { InitialDataContext } from "./src/InitialDataContext";
global.window = {};
const app = express();
/* Server is using the static files which is builded */
app.use(express.static("./build", { index: false }));
// Data
const articles = [
{ title: "Article 1", author: "Bob" },
{ title: "Article 2", author: "Betty" },
{ title: "Article 3", author: "Frank" },
];
// Route that we sent the data
app.get("/api/articles", (req, res) => {
res.json(articles);
});
// We saying all the styles should be applied for all routes right here
app.get("/*", async (req, res) => {
// Reaching styles in the app
const sheet = new ServerStyleSheet();
const contextObj = { _isServerSide: true, _requests: [], _data: {} };
renderToString(
// Covering the app with the styles
sheet.collectStyles(
<InitialDataContext.Provider value={contextObj}>
{/* Server-Side routing */}
<StaticRouter location={req.url}>
<App />
</StaticRouter>
</InitialDataContext.Provider>
)
);
await Promise.all(contextObj._requests);
contextObj._isServerSide = false;
delete contextObj._requests;
const reactApp = renderToString(
// Covering the app with the styles
<InitialDataContext.Provider value={contextObj}>
{/* // Server-Side routing */}
<StaticRouter location={req.url}>
<App />
</StaticRouter>
</InitialDataContext.Provider>
);
// We saying to the server that you need to render the html that end of the this path
const templateFile = path.resolve("./build/index.html");
fs.readFile(templateFile, "utf8", (err, data) => {
if (err) {
return res.status(500).send(err);
}
return res.send(
data
.replace(
'<div id="root"></div>',
`<script>window.preloadedData = ${JSON.stringify(
contextObj
)}</script><div id="root">${reactApp}</div>`
)
.replace("{{ styles }}", sheet.getStyleTags())
);
});
});
app.listen(8080, () => {
console.log("Server is running");
});