-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
110 lines (95 loc) · 3.07 KB
/
server.js
File metadata and controls
110 lines (95 loc) · 3.07 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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
require("dotenv").config({ override: true });
const { createServer } = require("https");
const { parse } = require("url");
const next = require("next");
const fs = require("fs");
const path = require("path");
const http = require("http");
const dev = process.env.NODE_ENV !== "production";
function resolveBindHost() {
// BIND_HOST avoids colliding with the shell's HOST variable (often unset or unrelated).
const configured = (process.env.BIND_HOST || process.env.HOST || "").trim();
if (configured) {
return configured;
}
return dev ? "localhost" : "0.0.0.0";
}
function resolveDisplayHostname(bindHost) {
const configured = (process.env.HOSTNAME || "").trim();
if (configured) {
return configured;
}
return bindHost === "0.0.0.0" ? "localhost" : bindHost;
}
function resolvePort() {
return parseInt(process.env.PORT || "3000", 10);
}
// SSL cert paths – set in .env or use defaults
const certPath =
process.env.SSL_CERT_PATH || path.join(__dirname, ".cert", "cert.pem");
const keyPath =
process.env.SSL_KEY_PATH || path.join(__dirname, ".cert", "key.pem");
function loadSslOptions() {
if (!fs.existsSync(certPath) || !fs.existsSync(keyPath)) {
return null;
}
try {
return {
key: fs.readFileSync(keyPath),
cert: fs.readFileSync(certPath),
};
} catch (err) {
console.warn(
`Could not read SSL certs at ${certPath} / ${keyPath}: ${err.message}. Starting HTTP instead.`
);
return null;
}
}
const port = resolvePort();
const bindHost = resolveBindHost();
const hostname = resolveDisplayHostname(bindHost);
const app = next({ dev, hostname, port });
const requestHandler = app.getRequestHandler();
app
.prepare()
.then(() => {
const sslOptions = loadSslOptions();
if (!sslOptions) {
console.warn(
`No usable SSL certs at ${certPath} / ${keyPath}. Starting HTTP.`
);
}
const server = sslOptions
? createServer(sslOptions, (req, res) => {
const parsedUrl = parse(req.url, true);
requestHandler(req, res, parsedUrl);
})
: http.createServer((req, res) => {
const parsedUrl = parse(req.url, true);
requestHandler(req, res, parsedUrl);
});
const listenHost = resolveBindHost();
const listenPort = resolvePort();
const displayHost = resolveDisplayHostname(listenHost);
const protocol = sslOptions ? "https" : "http";
console.log(`> Binding ${protocol} server to ${listenHost}:${listenPort}`);
server
.once("error", (err) => {
if (err && err.code === "EADDRINUSE") {
console.error(
`Port ${listenPort} is already in use on ${listenHost}. ` +
`Stop the other process, change PORT, or set BIND_HOST to a specific interface IP.`,
);
} else {
console.error(err);
}
process.exit(1);
})
.listen(listenPort, listenHost, () => {
console.log(`> Ready on ${protocol}://${displayHost}:${listenPort}`);
});
})
.catch((err) => {
console.error("Failed to start server:", err);
process.exit(1);
});