-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
48 lines (40 loc) · 1.04 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 fs from "fs";
import path from "path";
import { fileURLToPath } from "url";
import renderApp from "./dist/server/ServerApp.js";
const __dirname = path.dirname(fileURLToPath(import.meta.url));
const PORT = process.env.PORT || 3001;
const html = fs
.readFileSync(path.resolve(__dirname, "./dist/client/index.html"))
.toString();
const parts = html.split("not rendered");
const app = express();
app.use(
"/assets",
express.static(path.resolve(__dirname, "./dist/client/assets"))
);
app.use((req, res) => {
res.write(parts[0]);
const stream = renderApp(req.url, {
onShellReady() {
// if it is the crawler, do nothing here
stream.pipe(res);
},
onShellError() {
// do error handling
},
onAllReady() {
// if it is the crawler
// stream.pipe(res)
// last thing to write
res.write(parts[1]);
res.end();
},
onError(err) {
console.error(err);
},
});
});
console.log(`listening on http://localhost:${PORT}`);
app.listen(PORT);