-
Notifications
You must be signed in to change notification settings - Fork 56
/
s04-01-Nodejs.html
223 lines (139 loc) · 3.96 KB
/
s04-01-Nodejs.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
<!doctype html>
<html lang="sv">
<meta charset="utf-8" />
<title>webbsoft</title>
<!-- Mithril HTML Slideshow styles -->
<link href="css/mithril-slideshow.css" rel="stylesheet" />
<!-- Code formatting using highlight.js -->
<link rel="stylesheet" href="css/default.css">
<link rel="stylesheet" href="css/tomorrow.css">
<script src="js/highlight.pack.js"></script>
<!-- Text formatting using Markdown through showdown.js -->
<script src="js/showdown.min.js"></script>
<!-- Here comes the slides in order -->
<script data-role="slide" data-markdown type="text/html">
# Software Development for the Web
## Node.js
### Mikael Roos, dbwebb
</script>
<script data-role="slide" data-markdown type="text/html">
# Agenda
* JavaScript
* Node.js
* Programming paradigm
* Callback, promise, async/await
* About modules
* About connection to MySQL
* Code samples
</script>
<script data-role="slide" data-markdown type="text/html">
# JavaScript
* 1995, Netscape, Brendan Eich, 10 days
* ECMAScript standard
* ES5, ES5.1
* ES6 - ES10, ES2015 - ES2019
* Ref manual on MDN
</script>
<script data-role="slide" data-markdown type="text/html">
# Node.js
* Opensource, cross platform
* Ryan Dahl, 2009
* JS on the server was used previously that, but not that popular
* Google V8 js engine + event loop + I/O API
* 2010 npm
* Same language on client and server
* Own website and ref manual
</script>
<script data-role="slide" data-markdown type="text/html">
# Programming paradigm
* Event driven
* Asynchronous
* Blockande I/O
* Event loop
</script>
<script data-role="slide" data-markdown type="text/html">
# Callbacks
```
rl.question("What to search for? ", function(search) {
console.info(str);
});
```
```
rl.question("What to search for? ", (search) => {
console.info(str);
});
```
</script>
<script data-role="slide" data-markdown type="text/html">
# Promise
* A result of an asynchronous operation
* Pending... initial state
* Fulfilled... state of successful operation
* Rejected... state of failed operation
* Function returns a Promise()
</script>
<script data-role="slide" data-markdown type="text/html">
# Promise...
```
function readFile(filename, enc){
return new Promise(function (fulfill, reject) {
fs.readFile(filename, enc, function (err, res) {
if (err)
reject(err);
else
fulfill(res);
});
});
}
```
</script>
<script data-role="slide" data-markdown type="text/html">
# Async / await
* A function generating a Promise, can be used with await
* A function using await, must be defined as async
</script>
<script data-role="slide" data-markdown type="text/html">
# Async / await...
```
str = await readFile(filename, "UTF-8");
console.info(str);
```
</script>
<script data-role="slide" data-markdown type="text/html">
# Promisifying
* Add a layer of Promis on top of a callback function
* Allows to use async and await
* Use Node.js built-ins (util.promisfy)
* Genrally available on external libs (promisfy-ish)
</script>
<script data-role="slide" data-markdown type="text/html">
# Connect to MySQL
* Externt lib, via npm
* npm install mysql
* Layer of Promise (async/await)
* npm install promise-mysql
* Each lib has its own documentation
</script>
<script data-role="slide" data-markdown type="text/html">
# Moduler
* Functioner and classes kan be organised as modules
* Variants of working with modules
* Standardised "ES6 import", has limited support
* Node.js has require (lets use that)
</script>
<script data-role="slide" data-markdown type="text/html">
# Example programs
* Basic code structure
* Async and await
</script>
<script data-role="slide" data-markdown type="text/html">
# Last words
* Questions on that?
</script>
<script data-role="slide" data-markdown type="text/html">
<!-- empty slide by intention -->
</script>
<!-- include essential js-script -->
<script src="js/mithril.min.js"></script>
<script src="js/mithril-slideshow.js"></script>
</html>