-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
34 lines (28 loc) · 889 Bytes
/
Copy pathserver.js
File metadata and controls
34 lines (28 loc) · 889 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
const express = require('express');
const path = require('path');
const app = express();
const port = process.env.PORT || 3000;
// 🔐 Token-Schutz
const TOKEN = "meinSuperGeheimerToken123";
let lastCommand = "x";
// 📦 Middleware
app.use(express.json());
// 📄 Website direkt aus Hauptverzeichnis servieren
app.get('/', (req, res) => {
res.sendFile(path.join(__dirname, 'index.html'));
});
// ✅ API zum Setzen des Befehls
app.post('/api/control', (req, res) => {
const { token, cmd } = req.body;
if (token !== TOKEN) return res.status(403).send("Ungültiger Token");
lastCommand = cmd;
res.send("Befehl empfangen");
});
// ✅ API zum Abfragen durch Raspberry Pi
app.get('/api/state', (req, res) => {
res.send(lastCommand);
});
// 🚀 Server starten
app.listen(port, () => {
console.log(`Server läuft auf Port ${port}`);
});