fixed issues w/ accessing website on other computers

This commit is contained in:
2026-07-22 16:26:12 -06:00
parent c1e453d962
commit 8bb76d8e6c
13 changed files with 266 additions and 68 deletions
+43 -3
View File
@@ -1,10 +1,12 @@
import { createReadStream, realpathSync, statSync } from "node:fs";
import { createServer } from "node:http";
import { networkInterfaces } from "node:os";
import path from "node:path";
import { fileURLToPath } from "node:url";
const ROOT = realpathSync(path.dirname(fileURLToPath(import.meta.url)));
const HOST = process.env.HOST || "127.0.0.1";
const LAN_MODE = process.argv.includes("--lan");
const HOST = LAN_MODE ? "0.0.0.0" : process.env.HOST || "127.0.0.1";
const PORT = Number.parseInt(process.env.PORT || "4173", 10);
const TYPES = new Map([
[".css", "text/css; charset=utf-8"],
@@ -50,6 +52,28 @@ function safeFile(requestUrl) {
}
}
function displayUrls(host, port) {
if (host !== "0.0.0.0") {
const displayHost = host === "::" ? "::1" : host;
const urlHost = displayHost.includes(":") ? `[${displayHost}]` : displayHost;
return [{ label: "Open", url: `http://${urlHost}:${port}` }];
}
const entries = [{ label: "This computer", url: `http://127.0.0.1:${port}` }];
const seen = new Set();
for (const [interfaceName, addresses] of Object.entries(networkInterfaces())) {
for (const address of addresses || []) {
if (address.family !== "IPv4" || address.internal || seen.has(address.address)) continue;
seen.add(address.address);
entries.push({
label: `Other devices (${interfaceName})`,
url: `http://${address.address}:${port}`,
});
}
}
return entries;
}
const server = createServer((request, response) => {
if (!request.url || !["GET", "HEAD"].includes(request.method || "")) {
response.writeHead(405, { Allow: "GET, HEAD" });
@@ -66,7 +90,7 @@ const server = createServer((request, response) => {
const extension = path.extname(file).toLowerCase();
response.writeHead(200, {
"Content-Type": TYPES.get(extension) || "application/octet-stream",
"Cache-Control": extension === ".json" ? "no-store" : "public, max-age=300",
"Cache-Control": "no-store",
"X-Content-Type-Options": "nosniff",
"Referrer-Policy": "no-referrer",
});
@@ -77,6 +101,22 @@ const server = createServer((request, response) => {
createReadStream(file).on("error", () => response.destroy()).pipe(response);
});
server.on("error", (error) => {
if (error.code === "EADDRINUSE") {
process.stderr.write(
`Dashboard port ${PORT} is already in use. Stop the existing server or set a different PORT.\n`,
);
} else {
process.stderr.write(`Dashboard server could not start (${error.code || "unknown error"}).\n`);
}
process.exitCode = 1;
});
server.listen(PORT, HOST, () => {
process.stdout.write(`Utah Vehicle Health dashboard: http://${HOST}:${PORT}\n`);
const address = server.address();
const actualPort = typeof address === "object" && address ? address.port : PORT;
const urls = displayUrls(HOST, actualPort)
.map(({ label, url }) => ` ${label}: ${url}`)
.join("\n");
process.stdout.write(`Utah Vehicle Health dashboard:\n${urls}\n`);
});