58 lines
1.5 KiB
JavaScript
58 lines
1.5 KiB
JavaScript
import { existsSync, statSync } from "node:fs";
|
|
import { createServer } from "node:http";
|
|
import path from "node:path";
|
|
import { fileURLToPath, parse } from "node:url";
|
|
|
|
import next from "next";
|
|
|
|
const dirname = path.dirname(fileURLToPath(import.meta.url));
|
|
const port = Number.parseInt(process.env.PORT ?? "3910", 10);
|
|
const hostname = process.env.HOSTNAME ?? "0.0.0.0";
|
|
const dev = process.env.NODE_ENV !== "production";
|
|
|
|
const app = next({ dev, hostname, port });
|
|
const handle = app.getRequestHandler();
|
|
const nextStaticRoot = path.resolve(dirname, ".next/static");
|
|
|
|
function isMissingNextStaticAsset(pathname) {
|
|
if (!pathname.startsWith("/_next/static/")) {
|
|
return false;
|
|
}
|
|
|
|
let relativePath;
|
|
try {
|
|
relativePath = decodeURIComponent(pathname.replace("/_next/static/", ""));
|
|
} catch {
|
|
return true;
|
|
}
|
|
|
|
const assetPath = path.resolve(nextStaticRoot, relativePath);
|
|
if (!assetPath.startsWith(`${nextStaticRoot}${path.sep}`)) {
|
|
return true;
|
|
}
|
|
|
|
try {
|
|
return !existsSync(assetPath) || !statSync(assetPath).isFile();
|
|
} catch {
|
|
return true;
|
|
}
|
|
}
|
|
|
|
await app.prepare();
|
|
|
|
createServer((req, res) => {
|
|
const parsedUrl = parse(req.url ?? "/", true);
|
|
const pathname = parsedUrl.pathname ?? "/";
|
|
|
|
if (isMissingNextStaticAsset(pathname)) {
|
|
res.statusCode = 404;
|
|
res.setHeader("Content-Type", "text/plain; charset=utf-8");
|
|
res.end("Not found");
|
|
return;
|
|
}
|
|
|
|
handle(req, res, parsedUrl);
|
|
}).listen(port, hostname, () => {
|
|
console.log(`TicketTracker frontend listening on http://${hostname}:${port}`);
|
|
});
|