## Server-side Technology ### Architecture - Webserver - Static (only serving content) - Static and configurable - Dynamic (static + application server) - Storage System - SQL database - NoSQL database - Key-value store - Blob storage - File system ![](images/10-arch.png) ### Web Server #### Functionality - Task: - Communicate with browser - Implement HTTP - Possible architectures: - Process-based - Thread-based - State machine - Mixings/hybrid - Main loop: - Open a connection - Read the request - Process the request (and create the response) - Send the response - Close the connection #### Common Gateway Interface - Server uses `fork()` for the executable - Pass the connection to the subprocess - Programm fills a template by using database information #### 2nd Generation Framework (MVC) - Web server executes the *controller* **per request** - Workflow: - Pass the request to the controller - Parse the URL and the HTTP request to get them as parameters - Use these parameters to retrieve the model from the database - View component renders the template by using the model - Result: HTML - e.g., Ruby on Rails, Django - Note: JavaScript is just a resource on server-side #### 3rd Generation Framework (JS) - Usually, simple Web servers can be used to serve HTML, CSS and JS files - Requirements: - HTTP GET ist most dominant request type - Database operation - Further communication: - Model data (CRUD operations) - Access/modification of session data - **Result: Node.js** ##### Node.js - Concept: - Basis: JavaScript Runtime environment (*v8*) - Add events and management structures (handle all requests using the event loop) - Add interfaces to the OS (sockets, files, event handling) - Provide a useful module system (visibility of code) - Goal: - Single language used on client and server side - Supports two paradigms: - ==Really?== - Threads: - Requires scheduler - Operations are *blocking* (transparent to *waiting*) - Events: - Non-blocking - Fine-granular processing of events - Main loop: - Goal: Never wait or block in an event handling routine - Non-blocking ![](images/10-loop.png) ![](images/10-node-file.png) ##### Express.js - *Minimal* framework for Node.js - Features: - HTTP (also provided by Node.js) - Routing - Map URLs to functions - Routing table - Middleware - Manage sessions, cookies - Security - etc. - Express app: - Routing of HTTP requests - Rendering of templates (create HTML) - Interface to configure the middleware of the pre-processor ![](images/10-express.png) ###### Routing - `expressApp.get(urlPath, requestProcessfunction)` - `urlPath` may contain parameters - Other methods: - `get`, `post`, `put`, `delete`, `all` ###### Request Handling - e.g., `expressApp.get('/users', (request, response) => {...});` - Request object: - `params`: Parameters of the request - `query`: GET-Parameter - `body`: Content - `get(field)`: HTTP Header Parameter ###### Response Handling - `res.write(content)`: Write content - `res.status(code)`: Set status code - `res.set(prop, value)`: Set headers - `res.end()`: Finish and send response - `res.send(content)`: Equivalent to `write(content)` and `end()` - Supports chaining: - `res.status(code).write(c1).write(c2).end()` ###### Middleware - Important for flexibility (pre-process the request) - e.g., `app.all()` - Examples: - Check for user authentication - Parse/convert request - Session management - Cookie management - Compression - Encryption --- ### Storage Systems - Requirements - High availability (read and write) - High scalability - Concurrent access (e.g., transaction model) - Fault-tolerant (data loss would be critical) - Application data should be well-structured - e.g., fast access for model data - Easy usage - Systems: - Relational databases - NoSQL databases #### Relational Databases - Concepts: - Relations (tables) - Attributes (column) - Tuples (rows) - *Database schema*: Describe the structure of the database - Using SQL (descriptive query language) - **Object Relational Mapper** - Motivation: Relational model and web application do not always fit - Idea: Map OO model to relational model - Map objects to tuples - Map classes to relations - ORMs support the creation and evolution - e.g., Rails Active Records, SQLAlchemy #### NoSQL Database - *Not-only SQL* - Motivation: - NoSQL database fits better to the object-/document-based model of current Web applications - e.g., MongoDB - Data model: JSON - Expressive query language - Supports indexing - Scalable + reliable - **Schema-free or not?** - JSON-objects are flexible (no constraints) - Problem: - `

{{ person.name }}

` - `person.name` should be a character string and not too long - Ensure schema using validators (*integrity constraints*) - Push consistency-check into the application - e.g., Mongoose, Object Definition Language (ODL)