/
serve.ts
100 lines (86 loc) · 2.71 KB
/
serve.ts
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
import { getFaviconPath, resolveDist } from "@factor/engine/nodeUtils"
import { logger } from "@factor/api"
import { RenderMode, RenderOptions } from "@factor/types"
import compression from "compression"
import express, { Express } from "express"
import serveFavicon from "serve-favicon"
import serveStatic from "serve-static"
import * as vite from "vite"
import type { CliOptions } from "@factor/cli/program"
import { getRequestHtml, htmlGenerators } from "./render"
import { getViteServer } from "./vite"
export const expressApp = async (
options: Partial<RenderOptions> = {},
): Promise<Express> => {
const app = express()
try {
const { mode = "production", renderMode = RenderMode.SSR } = options
app.use(serveFavicon(getFaviconPath()))
let viteServer: vite.ViteDevServer | undefined = undefined
const { manifest, template } = await htmlGenerators(mode)
if (mode != "production") {
viteServer = await getViteServer(options)
app.use(viteServer.middlewares)
} else {
app.use(compression())
app.use(serveStatic(resolveDist("./client"), { index: false }))
}
// server side rendering
app.use("*", async (req, res) => {
const url = req.originalUrl
// This is the page catch all loader,
// If a file request falls through to this its 404
// make sure false triggers don't occur
const rawPath = url.split("?")[0]
if (rawPath.includes(".") && rawPath.split(".").pop() != "html") {
res.status(404).end()
return
}
try {
const html = await getRequestHtml({
template,
mode,
renderMode,
url,
manifest,
})
res.status(200).set({ "Content-Type": "text/html" }).end(html)
} catch (error: any) {
viteServer && viteServer.ssrFixStacktrace(error)
logger.log({
level: "error",
context: "server",
description: "ssr error",
data: error,
})
res.status(500).end(error.stack)
}
})
return app
} catch (error) {
logger.log({
level: "error",
context: "server",
description: "issue creating factor express app",
data: error,
})
return app
}
}
/**
* Serves a built app from [cwd]/dist
*/
export const serveApp = async (options: CliOptions = {}): Promise<void> => {
const { NODE_ENV } = options
const port = process.env.PORT || process.env.FACTOR_APP_PORT || "3000"
const appName = process.env.FACTOR_APP_NAME || "app"
const mode = NODE_ENV
const app = await expressApp({ mode })
app.listen(port, () => {
logger.log({
level: "info",
context: "server",
description: `${appName} @ http://localhost:${port}`,
})
})
}