-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.ts
55 lines (52 loc) · 1.69 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
44
45
46
47
48
49
50
51
52
53
54
55
import * as console from "node:console";
import * as fs from "node:fs/promises";
import type * as http from "node:http";
import * as https from "node:https";
import * as process from "node:process";
import * as url from "node:url";
import next from "next";
const hostname = "localhost.gojabako.zone";
const port = 3000;
const certsDir = new URL(`./certificates/${hostname}/`, import.meta.url);
const rootUrl = new URL(`https://${hostname}:${port}`);
const server = https.createServer({
key: await fs.readFile(new URL("privkey.pem", certsDir)),
cert: await fs.readFile(new URL("fullchain.pem", certsDir)),
});
server.once("error", (error) => {
console.error(error);
process.exit(1);
});
server.once("listening", () => console.info(`> Ready on ${rootUrl.href}`));
const nextDevServer = next({
dev: true,
dir: process.cwd(),
hostname,
port,
httpServer: server,
});
await nextDevServer.prepare();
const parseUrl = (requestPath = "/") => {
const parsedUrl = url.parse(new URL(requestPath, rootUrl).href, true);
return {
...parsedUrl,
query: parsedUrl.query || {},
pathname: parsedUrl.pathname ?? "",
};
};
const handleRequest = nextDevServer.getRequestHandler();
const onRequest = (req: http.IncomingMessage, res: http.ServerResponse) => {
const parsedUrl = parseUrl(req.url);
if (parsedUrl.pathname.startsWith("/_")) {
console.info(`debug: ${req.method} ${req.url}`);
} else {
console.info(`${req.method} ${req.url}`);
}
handleRequest(req, res, parsedUrl).catch(console.error);
};
server.on("request", onRequest);
const upgradeHandler = nextDevServer.getUpgradeHandler();
server.on("upgrade", (req, socket, head) => {
upgradeHandler(req, socket, head).catch(console.error);
});
server.listen(port);