-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
108 lines (92 loc) · 2.88 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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
import fs from "fs";
import express, { response } from "express";
import serialize from "serialize-javascript";
import compression from "compression";
import serveStatic from "serve-static";
import { fileURLToPath } from "url";
import { createServer as createViteServer } from "vite";
import { createRequire } from "module";
import authMiddleware from "./auth-middleware.mjs";
const isomorphicRequire = createRequire(import.meta.url);
const { auth, requiresAuth } = isomorphicRequire("express-openid-connect");
async function createServer(
root = process.cwd(),
isProd = process.env.NODE_ENV === "production"
) {
const brand = process.env.VITE_APP_BRAND;
const indexProd = isProd
? fs.readFileSync(
fileURLToPath(new URL("dist/client/index.html", import.meta.url)),
"utf-8"
)
: "";
const manifest = isProd
? isomorphicRequire("./dist/client/ssr-manifest.json")
: {};
const app = express();
let vite;
if (!isProd) {
vite = await createViteServer({
root,
logLevel: "info",
mode: brand,
server: {
middlewareMode: true,
watch: {
// During tests we edit the files too fast and sometimes chokidar
// misses change events, so enforce polling for consistency
usePolling: true,
interval: 100,
},
},
appType: "custom",
});
// use vite's connect instance as middleware
app.use(vite.middlewares);
} else {
app.use(compression());
app.use(
serveStatic(fileURLToPath(new URL("dist/client", import.meta.url)), {
index: false,
})
);
}
// add auth0 middleware
app.use(auth(authMiddleware.authConfig));
app.use(authMiddleware.authRouter);
app.use("*", async (req, res) => {
try {
const url = req.originalUrl;
let template, render;
if (!isProd) {
// always read fresh template in dev
template = fs.readFileSync(
fileURLToPath(new URL("index.html", import.meta.url)),
"utf-8"
);
template = await vite.transformIndexHtml(url, template);
render = (await vite.ssrLoadModule("/src/entry-server.ts")).render;
} else {
template = indexProd;
const entryServer = await import("./dist/server/entry-server.js");
render = entryServer.render;
}
const [appHtml, preloadLinks] = await render(url, manifest);
const html = template
.replace(`<!--ssr-preload-links-->`, preloadLinks)
.replace(`<!--ssr-app-html-->`, appHtml);
res.status(200).set({ "Content-Type": "text/html" }).end(html);
} catch (e) {
vite && vite.ssrFixStacktrace(e);
console.log(e.stack);
res.status(500).end(e.stack);
}
});
return { app, vite };
}
let port = process.env.PORT || 3000;
createServer().then(({ app }) => {
app.listen(port, () => {
console.log("listen on port ", port);
});
});