Initial commit
This commit is contained in:
commit
b82adc93c7
12 files changed
+193
No files matched your search
@@ -0,0 +1,40 @@
|
||||
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}`));
|
||||
Reference in new issue
Block a user