-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
36 lines (33 loc) · 1.07 KB
/
index.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
const { NODE_ENV = "development", PORT = 3000 } = process.env;
console.time("Start");
let address;
if (NODE_ENV === "production") {
// In production, simply start up the fastify server.
const { app } = await import("./dist/index.js");
address = await app.listen({ port: PORT });
} else {
// In dev we'll start a Vite dev server in middleware mode,
// and forward requests to our fastify server.
const { once } = await import("events");
const { createServer } = await import("vite");
const devServer = await createServer({
appType: "custom",
server: { middlewareMode: true },
});
const server = devServer.middlewares
.use(async (req, res, next) => {
try {
const { app } = await devServer.ssrLoadModule("./src/index.js");
await app.ready();
app.routing(req, res);
} catch (err) {
return next(err);
}
})
.listen(PORT);
await once(server, "listening");
address = `http://localhost:${server.address().port}`;
}
console.timeEnd("Start");
console.log(`Env: ${NODE_ENV}`);
console.log(`Address: ${address}`);