Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 0f9745c

Browse files
author
Noer Paanakker
committed
finished lesson plan week 3
1 parent f08f150 commit 0f9745c

File tree

1 file changed

+92
-33
lines changed

1 file changed

+92
-33
lines changed

week3/LESSONPLAN.md

Lines changed: 92 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -28,82 +28,141 @@ Additionally, middleware can either terminate the HTTP request or pass it on to
2828

2929
**Example**
3030

31-
Try out the following code and use Postman to make the request. Show the student the importance of `express.json()` to parse the request and makes the form data available in the `req.body`.
31+
Try out the following code and show how the middleware gets applied to the request, before it reaches the endpoint `/test`.
3232

3333
```js
3434
const express = require('express');
3535
const app = express();
3636

37-
app.use(express.json());
38-
app.post('/test', (req, res) => res.json(req.body));
37+
app.use(function (req, res, next) {
38+
console.log('hello from the middleware!');
39+
next();
40+
});
41+
42+
app.post('/test', (req, res) => res.send('Hello from test!'));
3943

4044
const PORT = process.env.PORT || 3000;
4145
app.listen(PORT, () => {
4246
console.log(`Server listening on port ${PORT}`);
4347
});
4448
```
4549

46-
**Exercise**
50+
Explain use cases for using middleware, like validation (`express-validator`) or parsing the request (`express.json()`)
4751

48-
**Essence**
52+
**Exercise**
4953

50-
Middleware allows the web server to modify the request gets in order to make it better interpretable within the route. For example,
54+
Ask students to create a simple Express server:
5155

52-
### Consuming web APIs
56+
- with one POST endpoint `/`.
57+
- This endpoint should receive form data (a single property, `email`) in JSON format.
58+
- To parse the request, have them use `express.json()`, as a middleware function.
59+
- Have them use Postman to test whether or not it works.
5360

54-
**Explanation**
61+
At the end of the exercise ask 1 or 2 students to show their approach.
5562

56-
Traditional server architecture one client one server that does anything:
57-
https://cloud.netlifyusercontent.com/assets/344dbf88-fdf9-42bb-adb4-46f01eedd629/27f810ea-2722-455a-9a0d-bb5b54c28393/api-based-platforms-api-diagram.png
63+
**Essence**
5864

59-
https://cdn.darknet.org.uk/wp-content/uploads/2018/08/HTTP-Security-Considerations-An-Introduction-To-HTTP-Basics.png
65+
Middleware allows the web server to modify the request gets in order to make it better interpretable within the route. For example, when sending form data in a request we want to make sure the server can understand the format it comes in properly. Therefore, we use the middleware `express.json()`.
6066

61-
In reality the server does not do everything on its own. Instead it uses services from other servers
62-
https://www.notion.so/gajduk/Hosting-b4025782198b494ba6bd053953c8933b#f8f31bc004ab46199639d914daad79fe
67+
### Consuming web APIs
6368

64-
Why do we need server-server communication?
69+
**Explanation**
6570

66-
- reuse - we do not want to write new code if someone has already done that in the past and we can just use it
67-
- separation-of-concerns - especially in big organizations like netflix etc
71+
Web applications are built using many different services. There's no need to have your application's do everything, from authentication to payment processing. To make things easier we use external services, also known as `web APIs`. Such a service can be used through their API, which allows us to get access to certain functionality and data, to use in our own application. This server to server communication through APIs is also known as `consumation` of web APIs.
6872

6973
**Example**
7074

71-
- Location services - https://api.postcode.nl/documentation/json-rest/v1/Address/viewByPostcode
72-
- Process payments (Stripe) - https://stripe.com/docs/api/invoices
75+
- Social login is a way for applications to outsource authentication, via services like Facebook or Google (examples are [Udemy](https://www.udemy.com/join/login-popup/), or [Medium](https://medium.com/))
76+
- Online payment processing is outsourced to services like Stripe or Adyen (examples are [Udemy](https://www.udemy.com/), or [bol.com](https://www.bol.com)))
7377

7478
**Exercise**
7579

76-
1. Get the image from https://randomfox.ca/floof/ and redirect to it
80+
Ask students to create a simple Express server:
7781

78-
2. Instead of redirecting show it inside HTML
82+
- With 1 GET endpoint `/github`
83+
- Inside the route, make an API request using `node-fetch` to `https://api.github.com/users/:username/repos`
84+
- Replace the `:username:` with your own GitHub user name
85+
- Respond to the client with the first repository that comes up
86+
- Use Postman to test your work
7987

80-
This is prelude to part 2, mention how it is ugly that the HTML and javascript are all mixed up
88+
**Essence**
89+
90+
Why write everything yourself, when you can make use of other web services? By consuming web APIs we can extend the usability of our application, without the need to do all the work ourselves!
8191

8292
SECOND HALF (14:00 - 16:00)
8393

8494
### Templating engines
8595

86-
**Explain**
96+
**Explanation**
8797

88-
[Templating engines](https://www.youtube.com/watch?v=oZGmHNZv7Sc)
98+
A templating engine is a technology that makes it possible to to create `dynamic` pages. Instead of writing regular HTML, you'll create `templates`. This is similar to HTML, but with one big difference: certain values serve as placeholders. These placeholders will be filled in with actual content, when the page is rendered. The type of content that is chosen depends on the person that's viewing it.
8999

90-
Motivation: make a story with a link to last exercise. The js, html and styling code are all intermixed in same file, it is a mess
100+
**Example**
91101

92-
Solution is to use a templating engine to separate the view from the node code but still use the data from node in the view
102+
A simple example of a Handlebars template:
103+
104+
```html
105+
<!DOCTYPE html>
106+
<html lang="en">
107+
<head>
108+
<meta charset="UTF-8" />
109+
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
110+
<title>Handlebars Example</title>
111+
</head>
112+
<body>
113+
<!-- The Handlebars template will be injected here -->
114+
<div id="entry-container"></div>
115+
116+
<!-- This script contains the Handlebars template. Notice the placeholders "title" and "body" -->
117+
<script id="entry-template" type="text/x-handlebars-template">
118+
<div class='entry'>
119+
<h1>
120+
{{title}}
121+
</h1>
122+
<div class='body'>
123+
{{body}}
124+
</div>
125+
</div>
126+
</script>
127+
128+
<!-- We need to load in the Handlebars package -->
129+
<script src="https://cdn.jsdelivr.net/npm/handlebars@latest/dist/handlebars.js"></script>
130+
131+
<!-- In this script we write the logic that compiles the template and injects it into the container -->
132+
<script>
133+
// Get access to DOM elements
134+
const container = document.getElementById('entry-container');
135+
// Access the HTML from inside the script
136+
const template = document.getElementById('entry-template').innerHTML;
137+
138+
// Use Handlebars to compile the template
139+
const compiledTemplate = Handlebars.compile(template);
140+
141+
// Content to be used in template
142+
const content = { title: 'I love...', body: 'HackYourFuture!!!!' };
143+
144+
// Inject the template into the container
145+
container.innerHTML = compiledTemplate(content);
146+
</script>
147+
</body>
148+
</html>
149+
```
93150

94-
How do templating engines work - they replace tokens/placeholders in a template string/file with actual data coming from json
151+
**Exercise**
95152

96-
How to use them in Node - https://www.npmjs.com/package/handlebars
153+
Ask students to get dynamically render content to an HTML page, using [Handlebars](http://handlebarsjs.com/). The HTML page should include:
97154

98-
**Example**
155+
- A complete HTML document
156+
- A CDN link to Handlebars: https://cdn.jsdelivr.net/npm/handlebars@latest/dist/handlebars.js
157+
- A JavaScript file that contains the content and the Handlebars template
158+
- Use the following object as the dynamic content: `{ question: "What's the best coding school in the world?" , answer: "HackYourFuture!" }`
159+
- A `<div>` that will contain the rendered Handlebars template
99160

100-
3. Use handlebars to refactor the page from exercise 2
161+
Make use of the [documentation](http://handlebarsjs.com/installation/#usage) to figure out how to do it!
101162

102-
**Exercise**
163+
**Essence**
103164

104-
- Use Handlebars to build a simple UI for reading books from the Library app
105-
- get the books with axios/fetch
106-
- EXTRA: buttons to create, edit, delete book
165+
Templating engines are a way to generate HTML with dynamically changing content.
107166

108167
# !!!IMPORTANT!!!
109168

0 commit comments

Comments
 (0)