Dies ist eine alte Version des Dokuments!
LU03.L01
app.js
// Author: Volkan Demir, 07.05.2026 // Erster kleiner Express-Server in JavaScript // Dieser Server liest die index.html Datei und sendet sie an den Browser, wenn die Startseite ("/") aufgerufen wird. const http = require("http"); const fs = require("fs"); const { text } = require("stream/consumers"); const PORT = 3001; const server = http.createServer(function(req, res) { res.writeHead(200, { "Content-Type": "text/html" }); //res.write(" Hallo Welt! "); //res.end(); fs.readFile("index.html", function (err, data) { if (err) { res.writeHead(404, { "Content-Type": "text/html" }); res.write("Datei nicht gefunden"); return; } else { res.write("<h1> Welt! Hier ist die index.html Datei von Demir: </h1>"); // Teilauftrag 1 res.write('<h2 style="color: red";>ich will hier raus!!!</h2>'); // Teilauftrag 2 // Anlegen und Anzeigen einer kleine Tabelle mittels JS-generierten HTML-Code htmlstr = '<table border=1ß>'; // beginning of the table htmlstr += '<tr><th>#</th><th>Name</th><th>Surename</th><th>Age</th></tr>'; // Header of the table htmlstr += '<tr><td>1</td><td>Demir</td><td>Volkan</td><td>57</t></tr>'; // Header of the table htmlstr += '<table>'; // End of the table res.write(htmlstr); } res.end(); }); }); server.listen(PORT, function() { console.log("Server läuft auf Port " + PORT); });
index.html
<!DOCTYPE html> <html lang="de"> <head> <meta charset="UTF-8"> <title>Node.js Server Nachricht</title> </head> <body> <h1>Antwort vom Server:</h1> <p>{SERVER_MSG}}<</p> </body> </html>
