- Previous homework & recap
- Middleware general concept (express.json)
- Error handling using middleware
- Consuming web APIs
- Templating engines
FIRST HALF (12:00 - 13:30)
Explain
Express middleware are functions that execute during the lifecycle of a request to the Express server. Each middleware has access to the HTTP request and response for each route (or path) it’s attached to. In fact, Express itself is compromised wholly of middleware functions. Additionally, middleware can either terminate the HTTP request or pass it on to another middleware function using next (more on that soon). This “chaining” of middleware allows you to compartmentalize your code and create reusable middleware.
**Examples **
express.json() - parses the body of type application/json a request and makes it available as a javascript object body-parser - parses the body of type form-data and amkes it available as javascript object
*Examples
- Sync error handling
- Async error handling using callbacks
- Async error handling using async/await
- Safeguarding
Explain
Why do we need server-server communication (reuse, separation-of-concerns)
Examples
- Location services - https://api.postcode.nl/documentation/json-rest/v1/Address/viewByPostcode
- Process payments (Stripe) - https://stripe.com/docs/api/invoices
Exercise
-
Get the image from https://randomfox.ca/floof/ and redirect to it
-
Instead of redirecting show in inside an html
This is prelude to part 2, mention how it is ugly that the HTML and javascript are all mixed up
SECOND HALF (14:00 - 16:00)
Explain
Motiviation, link to last exercise, js, html and styling all intermixed in same file, it is a mess
Solution is to use a templating engine to separrate the view from the node code but still use the data from node in the view
How do templating engines work
How to use them in Node
Example
- Use handlebars to refactor the page from exercise 2
Exercise
- Use handlebars to build a simple UI for reading books from the Library app
- get the books with axios/fetch
- EXTRA: buttons to create, edit, delete book
Ask students to prepare for databse course aby installing mySQL.