-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathserver.js
More file actions
85 lines (71 loc) · 2.37 KB
/
Copy pathserver.js
File metadata and controls
85 lines (71 loc) · 2.37 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
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
const { createServer } = await import("node:http");
const dev = process.env.NODE_ENV !== "production";
const port = Number(process.env.PORT ?? 3000);
const hostName = process.env.HOSTNAME || "AppExplorer.dev";
const next = await import("next");
const app = next.default({
dev,
hostname: hostName,
port,
conf: process.env.__NEXT_PRIVATE_STANDALONE_CONFIG
? JSON.parse(process.env.__NEXT_PRIVATE_STANDALONE_CONFIG)
: undefined,
});
const handler = app.getRequestHandler();
app
.prepare()
.then(async () => {
const httpServer = createServer(handler);
const nextUpgradeHandler = app.getUpgradeHandler();
await globalThis.socketHandler?.(httpServer);
httpServer.on("upgrade", (req, socket, head) => {
const url = new URL(req.url || "/", `https://${hostName}`);
const { pathname } = url;
// Pass HMR requests to Next.js's internal handler
if (pathname === "/_next/webpack-hmr") {
nextUpgradeHandler(req, socket, head);
} else {
// I think socket.io is handling this for me
}
});
httpServer.once("error", (err) => {
console.error(err);
process.exit(1);
});
httpServer.listen(port, () => {
const addressInfo = httpServer.address();
let serverUrl = "(unknown)";
if (typeof addressInfo === "string") {
serverUrl = `http://${addressInfo}`;
} else if (addressInfo && typeof addressInfo.port === "number") {
serverUrl = `http://${hostName}:${addressInfo.port}`;
} else {
console.error("Failed to determine server URL");
}
// eslint-disable-next-line no-console
console.log(`> Ready on ${serverUrl}`);
});
})
.catch((err) => {
console.error(err);
process.exit(1);
});
const metricsPort = Number(port + 1);
const metricsRegistry = globalThis.metricsRegistry;
const metricsServer = createServer(async (req, res) => {
if (req.url !== "/api/metrics" || !metricsRegistry) {
res.statusCode = 404;
res.end("not found");
return;
}
const body = await metricsRegistry?.asPrometheusText();
res.statusCode = 200;
res.setHeader("Content-Type", metricsRegistry?.contentType);
res.setHeader("Cache-Control", "no-store");
res.end(body);
});
metricsServer.listen(metricsPort, () => {
// eslint-disable-next-line no-console
console.log(`> Metrics Ready on http://localhost:${metricsPort}`);
});
export {};