-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
39 lines (30 loc) · 809 Bytes
/
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
import express from "express";
import React from "react";
import { renderToNodeStream } from "react-dom/server";
import { ServerLocation } from "@reach/router";
import fs from "fs";
import App from "./src/App";
const PORT = process.env.PORT || 3000;
const html = fs.readFileSync("dist/index.html").toString();
const parts = html.split("not rendered");
const app = express();
app.use("./dist", express.static("dist"));
app.use((req, res) => {
res.write(parts[0]);
const reactMarkup = (
<ServerLocation url={req.url}>
<App />
</ServerLocation>
);
const stream = renderToNodeStream(reactMarkup);
stream.pipe(
res,
{ end: false }
);
stream.on("end", () => {
res.write(parts[1]);
res.end();
});
});
console.log(`listening on port ${PORT}`);
app.listen(PORT);