From 131258aa868c58ebfa2311ae8073943dd10cb2df Mon Sep 17 00:00:00 2001 From: Christopher Hase Date: Fri, 11 Apr 2025 12:34:35 +0200 Subject: [PATCH] add http-server part 7.9.4 CORS --- backend/server.ts | 5 ++++- frontend/event.ts | 10 ++++++++-- 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/backend/server.ts b/backend/server.ts index 3d58828..29d9c7e 100644 --- a/backend/server.ts +++ b/backend/server.ts @@ -8,13 +8,16 @@ const port = 8090; //CORS aktivieren app.use(cors()); +// Verwende die Middleware, um POST-Body als JSON zu lesen +app.use(express.json()); + /*app.use(cors({ origin: 'http://localhost:8080' }));*/ app.post('/api/command', (req, res) => { executeCommand(); - res.status(200).send('Command executed'); + res.status(200).send('Command executed\n'); }); app.listen(port, '0.0.0.0', () => { diff --git a/frontend/event.ts b/frontend/event.ts index 21b0409..d01f4e2 100644 --- a/frontend/event.ts +++ b/frontend/event.ts @@ -3,9 +3,15 @@ function handleClick(): void { console.log("Der Button wurde geklickt!"); alert("Hallo von TypeScript!"); - fetch("http://localhost:8090/api/command", { method: "POST" }) + fetch("http://localhost:8090/api/command", { + method: "POST", + headers: { + "Content-Type": "application/json", // Füge dies hinzu, wenn du JSON sendest + } + }) .then(res => res.text()) - .then(text => console.log("Server sagt:", text)); + .then(text => console.log("Server sagt:", text)) + .catch(error => console.error("Fehler:", error)); /*fetch("/api/command", { method: "POST" }) .then(res => res.text())