| | |
| | | </code> |
| | | </pre> |
| | | </section> |
| | | <section> |
| | | <h3>TCP-Verbindungen</h3> |
| | | </section> |
| | | <section> |
| | | <ul> |
| | | <li>Bidirektionale Verbindung</li> |
| | | <li>Duplexfähig</li> |
| | | <li>Besteht immer aus Server- und Client-Socket</li> |
| | | </ul> |
| | | </section> |
| | | <section> |
| | | Mit dem nativen Modul <code>net</code> ist es möglich, eine TCP-Verbindung aufzubauen |
| | | <pre> |
| | | <code class="js" data-trim data-line-numbers> |
| | | /* Server */ |
| | | const net = require("net"); |
| | | |
| | | const server = net.createServer((socket) => { |
| | | |
| | | // new incoming connection |
| | | socket.on("data", (data) => { |
| | | // data is a buffer |
| | | }); |
| | | |
| | | socket.write("Hello World!"); |
| | | }); |
| | | |
| | | server.listen(3000); |
| | | </code> |
| | | </pre> |
| | | </section> |
| | | <section> |
| | | <pre> |
| | | <code class="js" data-trim data-line-numbers> |
| | | /* Client */ |
| | | const net = require("net"); |
| | | |
| | | const socket = net.connect(3000, () => { |
| | | // 'connect' listener |
| | | }); |
| | | |
| | | socket.on("data", (data) => { |
| | | // data is a buffer |
| | | }); |
| | | </code> |
| | | </pre> |
| | | </section> |
| | | <section> |
| | | <p>Aufgabe</p> |
| | | <p>Implementiere die Clientlogik in der <code>tcp-client.js</code>, um dem Server ein <code>"Hello Server!"</code> zurück zu senden</p> |
| | | </section> |
| | | <section> |
| | | <p>Aufgabe</p> |
| | | <p>Sende eine festgelegte Datei vom Server zum Client. Der Server liest die Datei ein und sendet die Inhalte zum Client, welcher diese wiederum in eine Zieldatei schreibt. Eventuell darf auch noch eine Kompression mittels Zip / Deflate implementiert werden.</p> |
| | | </section> |
| | | <section> |
| | | <p>Kurze Wiederholung des HTTP-Protokolls</p> |
| | | <p>Request</p> |
| | | <pre> |
| | | <code class="http" data-trim data-line-numbers> |
| | | GET / HTTP/1.1 |
| | | Host: localhost:3456 |
| | | </code> |
| | | </pre> |
| | | <p>Response</p> |
| | | <pre> |
| | | <code class="http" data-trim data-line-numbers> |
| | | HTTP/1.1 200 |
| | | Content-Type: text/plain |
| | | Content-Length: 12 |
| | | |
| | | Hello World! |
| | | </code> |
| | | </pre> |
| | | </section> |
| | | <section> |
| | | <p>Aufgabe</p> |
| | | <p>Implementiere mithilfe des TCP-Sockets einen minimalen HTTP-Server, welcher <code>Hello <name></code> als Text zurück gibt, wobei <code><name></code> aus dem Query-Parameter gelesen werden soll</p> |
| | | </section> |
| | | <section> |
| | | <h3>HTTP-Server</h3> |
| | | </section> |
| | | <section> |
| | | <pre> |
| | | <code class="js" data-trim data-line-numbers> |
| | | const http = require("http"); |
| | | |
| | | const server = http.createServer((req, res) => { |
| | | res.writeHeader(200, { |
| | | "Content-Type": "text/plain", /* Put Content-Type here */ |
| | | }); |
| | | |
| | | console.log(req.rawHeaders); |
| | | console.log(req.method); |
| | | console.log(req.url); |
| | | |
| | | res.write(/* response content */); |
| | | res.end(); /* important if no Content-Length is specified */ |
| | | }); |
| | | |
| | | server.listen(3456); |
| | | </code> |
| | | </pre> |
| | | </section> |
| | | <section> |
| | | <p>Aufgabe</p> |
| | | <p>Experimentiert mit verschiedenen Möglichkeiten von <code>Content-Length</code> und dem Vorhandensein von <code>res.end()</code>, mit längerer oder kürzerer <code>Content-Length</code> als der tatsächliche Body oder teilt den Body in zwei <code>res.write()</code> und verzögert künstlich mit einem setTimeout() den zweiten Teil und schaut euch im Browser bei den Entwickler-Tools die Timings der Response an.</p> |
| | | </section> |
| | | <section> |
| | | <h3>Worker Threads</h3> |
| | | </section> |
| | | <section> |
| | | <p>Was sind Worker Threads?</p> |
| | | <p>Eine bidirektionale nachrichten-basierte Implementierung von Multithreading in NodeJS</p> |
| | | </section> |
| | | <section> |
| | | <p>Beispiel:</p> |
| | | <pre> |
| | | <code class="js" data-trim data-line-numbers> |
| | | // Main Thread |
| | | const { Worker } = require('worker_threads'); |
| | | |
| | | const data = 'some data'; |
| | | |
| | | const worker = new Worker("path/to/worker.js", { workerData: data }); |
| | | |
| | | worker.on('message', message => console.log('Reply from Thread:', message)); |
| | | |
| | | // Worker Thread |
| | | const { workerData, parentPort } = require('worker_threads'); |
| | | |
| | | const result = workerData.toUpperCase(); |
| | | |
| | | parentPort.postMessage(result); |
| | | </code> |
| | | </pre> |
| | | </section> |
| | | <section> |
| | | <p>Weitere nützliche Funktionen:</p> |
| | | <pre> |
| | | <code class="js" data-trim data-line-numbers> |
| | | // Main Thread |
| | | worker.on('message', message => processMessage(message)); |
| | | worker.on('error', error => console.error(error)); |
| | | |
| | | worker.postMessage({a: 1}); // send any data to the worker |
| | | |
| | | worker.terminate(); // manually terminate worker thread |
| | | |
| | | // Worker Thread |
| | | parentPort.on('message', message => doSomething(message)); // receive messages from the main thread |
| | | |
| | | process.exit(0); // terminate self, optionally with exit code |
| | | </code> |
| | | </pre> |
| | | </section> |
| | | <section> |
| | | <p>Aufgabe</p> |
| | | <p> |
| | | Nutze die in den <code>examples</code> vorhandene Funktion <code>isPrime</code> (010-nodejs/mt-main.js und 010-nodejs/mt-worker.js) und implementiere mit Hilfe der <code>Worker Threads</code> die Suche nach Primzahlen. |
| | | Die gewünschte maximale Zahl, bis zu der die Primzahlen gesucht werden sollen als auch die Anzahl der Threads, mit der gleichzeitig gesucht werden soll, sollen möglichst einfach parametrisierbar sein (einfache Variable im Code genügt). |
| | | |
| | | Probiert verschiedene Thread-Anzahlen aus und vergleicht einmal die benötigten Zeiten. |
| | | </p> |
| | | </section> |
| | | <section> |
| | | <p>Hinweise:</p> |
| | | <ul> |
| | | <li>Bei der Suche bis zu 1.000.000 und 8 Threads soll der erste Thread von 0 bis 125.000 suchen, der zweite von 125.000 bis 250.000 usw.</li> |
| | | <li>Nutzt Promises, um auf alle Ergebnisse warten zu können. (s. <code>Promise.all</code>)</li> |
| | | <li>per <code>workerData</code> können auch Objekte übergeben werden</li> |
| | | <li>Die gefundenen Primzahlen können ausgegeben werden, müssen aber nicht. In erster Linie geht es darum, die Arbeit auf mehrere Threads aufzuteilen.</li> |
| | | </ul> |
| | | </section> |
| | | <section> |
| | | <h3>Multi Page Applications</h3> |
| | | </section> |
| | | <section> |
| | | <p>Im Gegensatz zu einer SPA ("Single Page Application") teilt sich eine Multi Page Application in mehrere Unterseiten auf, die sich per URL z.B. im Browser adressieren lassen.</p> |
| | | </section> |
| | | <section> |
| | | <p>In einem neuen Ordner / Projekt:</p> |
| | | <pre> |
| | | <code class="bash" data-trim data-line-numbers> |
| | | npm init -y |
| | | |
| | | npm install express pug |
| | | |
| | | mkdir public |
| | | </code> |
| | | </pre> |
| | | <pre> |
| | | <code class="js" data-trim data-line-numbers> |
| | | // index.js (s. examples 010-nodejs/mpa.js) |
| | | const express = require("express"); |
| | | const app = express(); |
| | | |
| | | app.use(express.static("public")); |
| | | |
| | | app.listen(3002); |
| | | </code> |
| | | </pre> |
| | | </section> |
| | | <section> |
| | | <h3>Aufgabe</h3> |
| | | <p>Fülle das Verzeichnis "public" mit jeweis einer HTML-Datei für eine "Home"- und eine "About Us"-Seite, die beide eine einfache Navigations-Leiste enthalten (simple <code>span</code> mit Link (href)), um zwischen diesen beiden Seiten navigieren zu können. |
| | | Unterscheiden sollen sich diese beiden Seiten durch ihren "Inhalt", indem dort einfach "Home" und "About Us" enthalten ist.</p> |
| | | </section> |
| | | <section> |
| | | <h3>Serverseitiges Rendern</h3> |
| | | </section> |
| | | <section> |
| | | <p>Template-Engine "pug"</p> |
| | | <pre> |
| | | <code class="css" data-trim data-line-numbers> |
| | | html |
| | | head |
| | | body |
| | | div This is a div with some text |
| | | #main.my-class This is also a div with short-hands for id and class |
| | | a(href="/") This is a link with href |
| | | </code> |
| | | </pre> |
| | | </section> |
| | | <section> |
| | | <p>Conditionals</p> |
| | | <pre> |
| | | <code class="css" data-trim data-line-numbers> |
| | | html |
| | | head |
| | | body |
| | | if name |
| | | div Hello #{name}! |
| | | else |
| | | div Hello Anonymous" |
| | | </code> |
| | | </pre> |
| | | </section> |
| | | <section> |
| | | <p>Extension / Inheritance</p> |
| | | <pre> |
| | | <code class="css" data-trim data-line-numbers> |
| | | // layout.pug |
| | | html |
| | | head |
| | | body |
| | | div Some static content |
| | | block content |
| | | |
| | | // page.pug |
| | | extends layout.pug |
| | | |
| | | block content |
| | | div This is my home content |
| | | </code> |
| | | </pre> |
| | | </section> |
| | | <section> |
| | | <p>Pug in Express</p> |
| | | <pre> |
| | | <code class="bash" data-trim data-line-numbers> |
| | | mkdir views |
| | | </code> |
| | | </pre> |
| | | <pre> |
| | | <code class="js" data-trim data-line-numbers> |
| | | // ... |
| | | |
| | | app.set('view engine', 'pug'); |
| | | |
| | | app.get('/', (req, res) => { |
| | | // file name without .pug and some data passed to the template |
| | | res.render('index', { title: 'Hey', message: 'Hello there!' }) |
| | | }); |
| | | |
| | | app.listen(3002); |
| | | </code> |
| | | </pre> |
| | | <pre> |
| | | <code class="css" data-trim data-line-numbers> |
| | | // views/index.pug |
| | | html |
| | | head |
| | | title #{title} |
| | | body #{message} |
| | | </code> |
| | | </pre> |
| | | </section> |
| | | <section> |
| | | <h3>Aufgabe</h3> |
| | | <p>Implementiere die Seiten aus dem vorherigen Beispiel mit Pug (nutze Vererbung, um die Navigationsleiste nicht doppelt implementieren zu müssen) und |
| | | baue einen Gruß im Seitentext mit dynamischen Namen ein, den man z.B. in der URL als Query-Parameter "?name=Joe" angibt und der im Server ausgewertet wird.</p> |
| | | </section> |
| | | </section> |
| | | </div> |
| | | </div> |