--- marp: true theme: gaia paginate: true backgroundColor: #fff header: "Web Engineering – DHBW Stuttgart" footer: "Michael Czechowski – SoSe 2026" --- # Node.js – Scripting, Running and Building --- # Node.js – Was ist das? - JavaScript Runtime (V8 Engine) - Server-side JavaScript - npm Package Manager - Ideal für: APIs, CLI Tools, Automation --- ![bg fit](./assets/demos/nodejs-usecases.png) --- # node --check und node -e ```bash # Syntax check only node --check script.js # One-liner node -e "console.log('Hallo Welt')" ``` --- # node -e Beispiele ```bash # String node -e "console.log('Hello ' + process.argv[2])" Welt # File reading node -e "console.log(require('fs').readFileSync('data.json'))" # JSON parsen node -e "console.log(JSON.parse(process.argv[1]))" '{"a":1}' ``` --- # require() – Module laden ```javascript // Eigene Datei const utils = require('./utils'); // npm Modul const express = require('express'); // Built-in const fs = require('fs'); const path = require('path'); ``` --- # __dirname und __filename ```javascript // Aktuelles Verzeichnis (Node.js) console.log(__dirname); // /home/user/project/src console.log(__filename); // /home/user/project/src/app.js // Pfad joinen const configPath = path.join(__dirname, '..', 'config.json'); ``` --- # HTTP Server mit Node.js (Built-in) ```javascript const http = require('http'); const server = http.createServer((req, res) => { res.writeHead(200, { 'Content-Type': 'text/html' }); res.end('

Hallo Welt!

'); }); server.listen(3000, () => { console.log('Server läuft auf Port 3000'); }); ``` --- # fs – Dateien lesen/schreiben ```javascript const fs = require('fs'); // Async fs.readFile('data.json', 'utf8', (err, data) => { if (err) throw err; console.log(JSON.parse(data)); }); // Sync const content = fs.readFileSync('data.json', 'utf8'); // Write fs.writeFile('output.txt', 'Hallo Welt', (err) => { if (err) throw err; }); ``` --- # package.json – Das Herzstück ```json { "name": "mein-projekt", "version": "1.0.0", "main": "index.js", "scripts": { "start": "node index.js", "dev": "node --watch index.js", "test": "node --test" }, "dependencies": { "express": "^4.18.0" }, "devDependencies": { "nodemon": "^3.0.0" } } ``` --- # npm Scripts ```bash # npm run