Unterschiede
Hier werden die Unterschiede zwischen zwei Versionen angezeigt.
| Beide Seiten der vorigen Revision Vorhergehende Überarbeitung Nächste Überarbeitung | Vorhergehende Überarbeitung | ||
| modul:m290:learningunits:lu08:theorie:02 [2024/10/29 13:11] – vdemir | modul:m290:learningunits:lu08:theorie:02 [2024/11/18 10:31] (aktuell) – cbolzern | ||
|---|---|---|---|
| Zeile 1: | Zeile 1: | ||
| - | ====== LU10c - Node.js | + | ====== LU10c - Node.js ====== |
| ===== Learning Objectives ===== | ===== Learning Objectives ===== | ||
| - | ... | + | - What a Node.js is |
| - | - ... | + | - Which benefits a node server provides us |
| + | - Program a node server | ||
| + | |||
| + | ===== Additional Materials ===== | ||
| + | * {{ : | ||
| + | |||
| + | |||
| + | ===== What is Node.js? ===== | ||
| + | A Node.js server is a backend system built using Node.js, a runtime environment that allows you to write server-side applications in JavaScript. This server can handle incoming requests, process data, communicate with databases, and return responses to the client (such as a web browser or mobile app). Here’s a breakdown of how a Node server works and why it’s popular: | ||
| + | |||
| + | - **JavaScript on the Server-Side: | ||
| + | - **Asynchronous, | ||
| + | - **Single-Threaded with Event Loop:** Unlike traditional multi-threaded servers, a Node server is single-threaded but uses an event loop to manage tasks. This architecture allows it to efficiently manage and prioritize incoming tasks without creating multiple threads, which reduces overhead and increases performance. | ||
| + | - **Wide Usage in Real-Time Applications: | ||
| + | - **Ecosystem and Libraries: | ||
| + | |||
| + | In short, a Node.js server provides a fast, efficient way to handle web requests, especially for applications that require real-time interactions and high scalability. | ||
| + | |||
| + | {{: | ||
| + | |||
| + | ===== Our first node server ===== | ||
| + | /* ******************************************************************************************* | ||
| + | * Author: V. Demir, 30.10.2024 | ||
| + | * ******************************************************************************************* | ||
| + | * Description: | ||
| + | * Express-Server, | ||
| + | * database, and provides the browser with the requested data. | ||
| + | * ******************************************************************************************* | ||
| + | * Hints: | ||
| + | * Before programming the server the following list of JS-Modules | ||
| + | * must be imported to our IDE. Execute the commands below in the terminal! | ||
| + | * npm install node | ||
| + | * npm init -y | ||
| + | * npm install mysql | ||
| + | * npm install body-parser | ||
| + | * npm install express | ||
| + | ** ***************************************************************************************** */ | ||
| + | // Reference: www.npmjs.com/ | ||
| | | ||
| + | const mysql = require(" | ||
| + | const express = require(' | ||
| + | | ||
| + | const port = 3000; // ThKind of a telefon number, on which the server listens for requests | ||
| + | | ||
| + | // Variable set with the db-connection credentials to connect the server to the database | ||
| + | const config = { | ||
| + | host: ' | ||
| + | database: ' | ||
| + | user: " | ||
| + | password: ' | ||
| + | } | ||
| + | | ||
| + | const connection = mysql.createConnection(config) | ||
| + | // connection line to the database, that takes the credentials | ||
| + | // and returns a open line to execute sql statements | ||
| + | | ||
| + | // function that actually connects the server to the database | ||
| + | // result is successful or errormessage | ||
| + | connection.connect(function(err) { | ||
| + | if (err) throw err; | ||
| + | console.log(' | ||
| + | | ||
| + | // Preparation of the sql statement, which will be send to the database | ||
| + | var sqlstmt = ' | ||
| + | | ||
| + | | ||
| + | // the sqlstmt is send to the database | ||
| + | if (err) throw err; // if everthing is fine, i receive the resultset | ||
| + | |||
| + | | ||
| + | | ||
| + | }); | ||
| + | }); | ||
| + | |||
| + | |||
| + | |||
| + | |||
| + | |||
| + | |||
| ==== Vocabulary ==== | ==== Vocabulary ==== | ||
| ^English ^ Deutsch ^ | ^English ^ Deutsch ^ | ||
| - | | ... | ... | | + | | scalable |
| - | | ... | ... | | + | |
| ---- | ---- | ||
| [[https:// | [[https:// | ||