41 lines
1.0 KiB
JavaScript
41 lines
1.0 KiB
JavaScript
import express from "express";
|
|
import fs from "fs";
|
|
import path from "path";
|
|
import { fileURLToPath } from "url";
|
|
|
|
const __dirname = path.dirname(fileURLToPath(import.meta.url));
|
|
const app = express();
|
|
app.use(express.json());
|
|
app.use(express.static(path.join(__dirname, "public")));
|
|
|
|
const stateFile = path.join(__dirname, "state.json");
|
|
|
|
// Initialzustand laden oder erzeugen
|
|
function getState() {
|
|
if (!fs.existsSync(stateFile)) {
|
|
fs.writeFileSync(stateFile, JSON.stringify({ current: "A" }, null, 2));
|
|
}
|
|
return JSON.parse(fs.readFileSync(stateFile));
|
|
}
|
|
|
|
function setState(newState) {
|
|
fs.writeFileSync(stateFile, JSON.stringify(newState, null, 2));
|
|
}
|
|
|
|
// GET: aktuellen Zustand
|
|
app.get("/status", (req, res) => {
|
|
res.json(getState());
|
|
});
|
|
|
|
// POST: wechseln
|
|
app.post("/toggle", (req, res) => {
|
|
const state = getState();
|
|
state.current = state.current === "A" ? "B" : "A";
|
|
setState(state);
|
|
res.json(state);
|
|
});
|
|
|
|
// Start
|
|
const PORT = process.env.PORT || 3000;
|
|
app.listen(PORT, () => console.log(`Zahl-Schalter läuft auf Port ${PORT}`));
|