-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.ts
43 lines (36 loc) · 1.16 KB
/
server.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
// server.ts
import { Hono } from "hono";
import { serve } from "@hono/node-server";
import { serveStatic } from "@hono/node-server/serve-static";
import { readFile } from "node:fs/promises";
const isProd = process.env["NODE_ENV"] === "production";
let html = await readFile(isProd ? "build/index.html" : "index.html", "utf8");
if (!isProd) {
// Inject Vite client code to the HTML
html = html.replace(
"<head>",
`
<script type="module">
import RefreshRuntime from "/@react-refresh"
RefreshRuntime.injectIntoGlobalHook(window)
window.$RefreshReg$ = () => {}
window.$RefreshSig$ = () => (type) => type
window.__vite_plugin_react_preamble_installed__ = true
</script>
<script type="module" src="/@vite/client"></script>
`
);
}
const app = new Hono()
.use("/assets/*", serveStatic({ root: isProd ? "build/" : "./" })) // path must end with '/'
.get("/*", (c) => c.html(html));
app.use("*", async (c, next) => {
c.res.headers.set("X-Powered-By", "Hono");
await next();
});
export default app;
if (isProd) {
serve({ ...app, port: 4000 }, (info) => {
console.log(`Listening on http://localhost:${info.port}`);
});
}