| Zeile 198: |
Zeile 198: |
| | * [https://www.airpair.com/javascript/node-js-tutorial Tutorial für Node.js] | | * [https://www.airpair.com/javascript/node-js-tutorial Tutorial für Node.js] |
| | | | |
| − | | + | ==== Installation ==== |
| − | ==== Node.js auf Debian Stretch ==== | + | ===== Node.js auf Debian Stretch ===== |
| | | | |
| | Node.js lässt sich am besten vom NodeSource.com repository installieren. Dazu eine Datei <code>/etc/apt/sources.list.d/nodesource.list</code> mit folgendem Inhalt anlegen: | | Node.js lässt sich am besten vom NodeSource.com repository installieren. Dazu eine Datei <code>/etc/apt/sources.list.d/nodesource.list</code> mit folgendem Inhalt anlegen: |
| Zeile 209: |
Zeile 209: |
| | sudo aptitude update && sudo aptitude install nodejs | | sudo aptitude update && sudo aptitude install nodejs |
| | | | |
| − | ==== Node.js auf Ubuntu / Linux Mint ==== | + | ===== Node.js auf Ubuntu / Linux Mint ===== |
| | | | |
| | Bei Installation Node.js auf einer [[Ubuntu]]-basierten Linux-Distribution ist wohl folgender Befehl zur Erstellung eines Softlinks notwendig:<ref>Siehe http://stackoverflow.com/questions/26320901/cannot-install-nodejs-usr-bin-env-node-no-such-file-or-directory</ref> | | Bei Installation Node.js auf einer [[Ubuntu]]-basierten Linux-Distribution ist wohl folgender Befehl zur Erstellung eines Softlinks notwendig:<ref>Siehe http://stackoverflow.com/questions/26320901/cannot-install-nodejs-usr-bin-env-node-no-such-file-or-directory</ref> |
| | | | |
| | sudo ln -s /usr/bin/nodejs /usr/bin/node | | sudo ln -s /usr/bin/nodejs /usr/bin/node |
| | + | |
| | + | ==== Einzelne Module von Node.js ==== |
| | + | |
| | + | ===== http - eingebauter WebServer ===== |
| | + | |
| | + | Um Anfragen von außerhalb des <code>localhost</code> zu blocken, muss man in der <code>listen()</code>-Methode nach dem Port die Adresse des localhost (<code>"127.0.0.1"</code>) angeben.<ref>Vgl. [https://nodejs.org/api/net.html#net_server_listen API-Dokumentation von listen()]</ref> Beispiel: |
| | + | |
| | + | <pre> |
| | + | server.listen(port, "127.0.0.1", (err) => { |
| | + | if (err) { |
| | + | return console.log('something bad happened', err) |
| | + | } |
| | + | |
| | + | console.log(`Server is listening on ${port}`) |
| | + | }) |
| | + | </pre> |
| | | | |
| | === Mini-Web-Server === | | === Mini-Web-Server === |