-
Notifications
You must be signed in to change notification settings - Fork 41
/
server.js
58 lines (52 loc) · 1.54 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
import compression from "compression";
import express from "express";
import morgan from "morgan";
// Short-circuit the type-checking of the built output.
const BUILD_PATH = "./build/server/index.js";
const DEVELOPMENT = process.env.NODE_ENV === "development";
const PORT = Number.parseInt(process.env.PORT || "3000");
const app = express();
app.use(compression());
app.disable("x-powered-by");
if (DEVELOPMENT) {
console.log("Starting development server");
const viteDevServer = await import("vite").then((vite) =>
vite.createServer({
server: { middlewareMode: true },
})
);
app.use(viteDevServer.middlewares);
app.use(async (req, res, next) => {
try {
const source = await viteDevServer.ssrLoadModule("./server/app.ts");
return await source.app(req, res, next);
} catch (error) {
if (typeof error === "object" && error instanceof Error) {
viteDevServer.ssrFixStacktrace(error);
}
next(error);
}
});
} else {
console.log("Starting production server");
app.use(
"/assets",
express.static("build/client/assets", { immutable: true, maxAge: "1y" })
);
app.use(
// browser 1 hour, server 1 year
express.static("build/client", {
setHeaders: (res) => {
res.setHeader(
"Cache-Control",
"public, max-age=3600, s-maxage=31536000"
);
},
})
);
app.use(await import(BUILD_PATH).then((mod) => mod.app));
}
app.use(morgan("tiny"));
app.listen(PORT, () => {
console.log(`Server is running on http://localhost:${PORT}`);
});