45 lines
1.1 KiB
JavaScript
45 lines
1.1 KiB
JavaScript
const express = require("express");
|
|
const path = require("path");
|
|
const fs = require("fs");
|
|
const isDev = process.env.NODE_ENV !== "production";
|
|
let livereload, connectLivereload;
|
|
if (isDev) {
|
|
livereload = require("livereload");
|
|
connectLivereload = require("connect-livereload");
|
|
}
|
|
const app = express();
|
|
const PORT = 3000;
|
|
const DOMAIN_FILE = path.join(__dirname, "domain.txt");
|
|
|
|
function readWsDomain() {
|
|
try {
|
|
const val = fs.readFileSync(DOMAIN_FILE, "utf8").trim();
|
|
if (val) return val;
|
|
} catch (err) {
|
|
if (err.code !== "ENOENT") {
|
|
console.warn("Failed to read domain.txt:", err.message);
|
|
}
|
|
}
|
|
return process.env.WS_DOMAIN || "localhost:8080";
|
|
}
|
|
|
|
if (isDev) {
|
|
const liveReloadServer = livereload.createServer();
|
|
liveReloadServer.watch(path.join(__dirname, "public"));
|
|
app.use(connectLivereload());
|
|
}
|
|
|
|
app.use(express.static("public"));
|
|
|
|
app.get("/ws-domain", (_req, res) => {
|
|
res.json({ domain: readWsDomain() });
|
|
});
|
|
|
|
app.get("/", (req, res) => {
|
|
res.sendFile(path.join(__dirname, "public", "index.html"));
|
|
});
|
|
|
|
app.listen(PORT, () => {
|
|
console.log(`Client server running at http://localhost:${PORT}`);
|
|
});
|