-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
49 lines (40 loc) · 1.18 KB
/
Copy pathserver.js
File metadata and controls
49 lines (40 loc) · 1.18 KB
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
const { NODE_ENV = "development", PORT = 3000 } = process.env;
let address;
console.time("Start");
if (NODE_ENV === "production") {
// In production, start up the fastify server.
const { app } = await import("./dist/index.js");
address = await app.listen({ port: PORT });
await app.ready();
} else {
// In dev 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 },
logger: {
level: 'info',
transport: {
target: 'pino-pretty'
}
}
});
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}`);