forked from kmoskwiak/useSSE
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathServer.jsx
50 lines (40 loc) · 1.15 KB
/
Server.jsx
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
import express from "express";
import React from "react";
import { renderToNodeStream, renderToString } from "react-dom/server";
import App from "./App";
import AppWithTimeout from "./AppWithTimeout";
import pageParts from "./index.html";
import path from "path";
import { createServerContext } from "use-sse";
const app = express();
app.use("/static", express.static(path.resolve(__dirname, "./build")));
app.use("/", async (req, res) => {
if (req.url !== "/") {
return res.status(404).end();
}
const { ServerDataContext, resolveData } = createServerContext();
res.write(pageParts[0]);
renderToString(
<ServerDataContext>
<App />
<AppWithTimeout />
</ServerDataContext>
);
const data = await resolveData(3000);
const htmlStream = renderToNodeStream(
<ServerDataContext>
<App />
<AppWithTimeout />
</ServerDataContext>
);
htmlStream.pipe(res, { end: false });
htmlStream.on("end", () => {
res.write(pageParts[1]);
res.write(data.toHtml());
res.write(pageParts[2]);
res.end();
});
});
app.listen(3000, "0.0.0.0", () => {
console.log(`${new Date()} Server listening on port 3000`);
});