-
Notifications
You must be signed in to change notification settings - Fork 13
/
server.ts
43 lines (35 loc) · 965 Bytes
/
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
/// <reference types="vite/client" />
import express, { RequestHandler } from "express";
import viteDevServer from "vavite/vite-dev-server";
console.log("Running the server");
const app = express();
// This is an optional optimization to load routes lazily so that
// when reloadOn option is set to "static-deps-change",
// changes to the route handlers will not trigger a reload.
// Feel free to remove this and import routes directly.
function lazy(
importer: () => Promise<{ default: RequestHandler }>,
): RequestHandler {
return async (req, res, next) => {
try {
const routeHandler = (await importer()).default;
routeHandler(req, res, next);
} catch (err) {
if (err instanceof Error) viteDevServer?.ssrFixStacktrace(err);
next(err);
}
};
}
app.get(
"/",
lazy(() => import("./routes/home")),
);
app.get(
"/foo",
lazy(() => import("./routes/foo")),
);
app.get(
"/bar",
lazy(() => import("./routes/bar")),
);
export default app;