/** * start.js - safe server launcher * Kills any process already on PORT before starting index.js. * Run with: node web/server/start.js */ const { execSync, spawnSync, spawn } = require("child_process"); const rawPort = process.env.PORT || "5000"; const PORT = parseInt(rawPort, 10); if (!Number.isInteger(PORT) || PORT < 1 || PORT > 65535) { console.error(`[start] Invalid PORT value: ${JSON.stringify(rawPort)}`); process.exit(1); } function killPort(port) { try { if (process.platform === "win32") { const out = execSync(`netstat -ano | findstr :${port}`, { encoding: "utf-8" }).trim(); const pids = new Set(); for (const line of out.split("\n")) { if (!line.includes("LISTENING")) continue; const pid = line.trim().split(/\s+/).pop(); if (pid && pid !== "0") pids.add(pid); } for (const pid of pids) { console.log(`[start] Killing stale process PID ${pid} on port ${port}`); spawnSync("taskkill", ["/PID", pid, "/F"], { stdio: "ignore" }); } } else { spawnSync("fuser", [`${port}/tcp`, "-k"], { stdio: "ignore" }); } } catch { // No process on that port -- fine } } killPort(PORT); const proc = spawn(process.execPath, [require("path").join(__dirname, "index.js")], { stdio: "inherit", env: process.env, }); proc.on("exit", (code) => process.exit(code ?? 0)); process.on("SIGINT", () => proc.kill("SIGINT")); process.on("SIGTERM", () => proc.kill("SIGTERM"));