This repository has been archived by the owner on Apr 19, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
574 lines (455 loc) · 16.8 KB
/
index.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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>ECE AST - NodeJS</title>
<meta name="description" content="ECE NodeJS class lesson on NodeJS">
<meta name="author" content="César Berezowski">
<meta name="apple-mobile-web-app-capable" content="yes">
<meta name="apple-mobile-web-app-status-bar-style" content="black-translucent">
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no, minimal-ui">
<link rel="stylesheet" href="../reveal.js/css/reveal.css">
<link rel="stylesheet" href="../reveal.js/css/theme/black.css" id="theme">
<!-- Code syntax highlighting -->
<link rel="stylesheet" href="../reveal.js/lib/css/zenburn.css">
<!-- Printing and PDF exports -->
<script>
var link = document.createElement( 'link' );
link.rel = 'stylesheet';
link.type = 'text/css';
link.href = window.location.search.match( /print-pdf/gi ) ? '../reveal.js/css/print/pdf.css' : '../reveal.js/css/print/paper.css';
document.getElementsByTagName( 'head' )[0].appendChild( link );
</script>
<!--[if lt IE 9]>
<script src="lib/js/html5shiv.js"></script>
<![endif]-->
</head>
<body>
<div style="position:absolute; top:20px; left:20px;">
<p><img src="../img/adaltas.png" width="200px" style="margin: 10px 0" /></p>
</div>
<div class="reveal">
<!-- Any section element inside of this container is displayed as a slide -->
<div class="slides">
<section>
<h1>NodeJS</h1>
<h4>Asynchronous Server Technologies</h4>
<p>
Sergei Kudinov<br/>
<i>Web Developer @ Adaltas</i><br/>
<i>[email protected]</i>
</p>
</section>
<section data-markdown>
## About this course
* You will use / do :
* JavaScript / TypeScript & Markdown
* Node.JS & NPM or yarn
* Git & Github / GitLab
* Unit tests & Travis CI
* Use a framework & transpiler
* Use a storage (LevelDB)
* Read the doc ! Ask Google !
* You will apprehend tools of the Open Source
</section>
<section data-markdown>
## About this course
* Evaluation :
* Continuous using Git
* Simple project at the end of the course
* Planning: [CALENDAR.md](../CALENDAR.md)
</section>
<section data-markdown>
## Final project
* Based on code from class
* Using presented technologies
* Simple dashboard app :
* User login
* A user can insert simple metrics
* A user can retrieve his metrics displayed nicely in a graph
* A user can only access his own metrics
* See [PROJECT.md](../PROJECT.md)
</section>
<section data-markdown>
## Questions ?
</section>
<section data-markdown>
## In this module
* Part 1: Node.js
* Creating "Hello world" project
* Creating HTTP server
* Routing with query parameters
* Part 2: Dependency managment in Node.js
* Node.js modules
* Packages
* Using Nodemon utility
</section>
<section data-markdown>
## JavaScript
* Developed in 1995 at NetScape
* Shipped with IE3 in 1996 as JScript
* Standardized with EcmaScript (ES) v1 in 1997 (now v8)
* No relation to Java
* Rediscovered with Ajax around 2005 (Gmail, Maps…)
* Multi-paradigm : scripting, object-oriented, functional, imperative, event-driven
* One of the most popular languages today
</section>
<section data-markdown>
## Node.JS
* JavaScript runtime for server-side scripting
* Created in 2009 by Ryan Dahl
* Uses Google's V8 JavaScript Engine
* Package management using NPM
* Asynchronous IO
* Unix philosophy of small components
* Community
* on Github
* backed up by the Node.js foundation
* Current version is v12.4.0 & LTS is 10.16.0
</section>
<section data-markdown>
## Hello world !
```javascript
// Import a module
const http = require('http')
// Declare an http server
http.createServer(function (req, res) {
// Write a response header
res.writeHead(200, {'Content-Type': 'text/plain'});
// Write a response content
res.end('Hello World\n');
// Start the server
}).listen(8080)
// curl localhost:8080 or go to http://localhost:8080
```
</section>
<section data-markdown>
## Callback functions
* What makes JavaScript and NodeJS asynchronous
```javascript
const serverHandle = function (req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('Hello World\n');
}
const server = http.createServer(serverHandle);
server.listen(8080)
```
</section>
<section data-markdown>
## Sending back HTML
* Change the content type & response content
```javascript
const content = '<!DOCTYPE html>' +
'<html>' +
' <head>' +
' <meta charset="utf-8" />' +
' <title>ECE AST</title>' +
' </head>' +
' <body>' +
' <p>Hello World !</p>' +
' </body>' +
'</html>'
const serverHandle = function (req, res) {
res.writeHead(200, {'Content-Type': 'text/html'});
res.write(content);
res.end();
}
```
</section>
<section data-markdown>
## Routing
* Define endpoints to serve multiple pages
* A route = an url [+ parameters] + a handler
* Use node's `url` module to parse `req.url`
</section>
<section data-markdown>
## Get the current path
```javascript
// Import Node url module
const url = require('url')
const serverHandle = function (req, res) {
// Retrieve and print the current path
const path = url.parse(req.url).pathname;
console.log(path);
res.writeHead(200, {'Content-Type': 'text/html'});
res.write(content);
res.end();
}
```
</section>
<section data-markdown>
## Query parameters
* Web URL can be enriched with query parameters
* After the `?`
* Separated by `&`
* Formatted as `key=value`
```
http://my.site/my/page.html?a=cesar&b=berezowski
```
* Parseable with node's `querystring` module on url's query property
</section>
<section data-markdown>
## Get query parameters
```javascript
const url = require('url')
const qs = require('querystring')
const serverHandle = function (req, res) {
// Retrieve and print the queryParams
const queryParams = qs.parse(url.parse(req.url).query);
console.log(queryParams);
res.writeHead(200, {'Content-Type': 'text/html'});
res.write(content);
res.end();
}
```
</section>
<section data-markdown>
## Basic routing example
```javascript
const url = require('url')
const qs = require('querystring')
const serverHandle = function (req, res) {
const route = url.parse(req.url)
const path = route.pathname
const params = qs.parse(route.query)
res.writeHead(200, {'Content-Type': 'text/plain'});
if (path === '/hello' && 'name' in params) {
res.write('Hello ' + params['name'])
} else {
res.write('Hello anonymous')
}
res.end();
}
```
</section>
<section data-markdown>
## Questions ?
</section>
<section data-markdown>
## Work
* Create a basic app with three routes:
* `/` explains how `/hello` works
* `/hello` takes a `name` query parameter and
* random names replies `hello [name]`
* your own name replies with a short intro of yourself
* any other replies a 404 code with a not found message
</section>
<section data-markdown>
## Dependency managment in Node.js
</section>
<section data-markdown>
## What is a Node.js module ?
* What we call a library in other languages
* One or more `.js` files doing something
* Module content is "exported"
</section>
<section data-markdown>
## Creating Modules
* Use
```javascript
module.exports = ...
```
* Export anything: a function, an array, an object...
```javascript
// for an object
module.exports = {
a: ...,
b: ...
}
// or
module.exports.a = ...
module.exports.b = ...
```
* Import in another file: (NB: **no extension**)
```javascript
const my_mod = require('/path/to/my_file')
```
</section>
<section data-markdown>
## Let's create a module !
```javascript
// ./handles.js
// Necessary imports
module.exports = {
serverHandle: function (req, res) {...}
}
```
```javascript
// ./index.js
const http = require('http')
const handles = require('./handles')
const server = http.createServer(handles.serverHandle);
server.listen(8080)
```
</section>
<section data-markdown>
## Dependency Management
* Download and install existing packages
* State versions used by your app
* Not reinvent the wheel
* Participate to the community
</section>
<section data-markdown>
## NPM
* Package manager for Node.JS
* Developed by Isaac Z. Schlueter
* Upload, share & download packages
* Two modes: global & local
* Modules: system I/O, networking, cryptography, framework, …
* [npmjs.com](http://npmjs.com)
</section>
<section data-markdown>
## yarn
* Alternative package manager for Node.JS
* Developed & open-sourced by Facebook
* Introduced novelties recuperated by NPM v4 & 5
* Equivalent nowadays
</section>
<section data-markdown>
## Install dependencies
* Add using NPM / yarn / package.json (manual)
* Specify version
* Install locally / globally
```shell
npm i[nstall] [--save] [--save-dev] [-g] package_name
yarn add package_name [--dev]
```
* Installation location: node_modules
</section>
<section data-markdown>
## Module declaration
`package.json`: stores a module's informations
* `name`, `description`, `version`, `license` and `private`
* `dependencies` and `devDependencies`
* scripts and commands
```json
{
"name": "ece-ast",
"description": "NodeJS project for ECE class",
"version": "0.1.0",
"license": "UNLICENSED",
"private": true,
"dependencies": {},
"devDependencies": {}
}
```
[package.json documentation](https://docs.npmjs.com/files/package.json)
</section>
<section data-markdown>
## Module declaration
Interactive `package.json` creation with
```shell
npm init
yarn init
```
[package.json doc](https://docs.npmjs.com/files/package.json)
</section>
<section data-markdown>
## Dependencies declaration
* dependencies: necessary to make your app run
```json
{ "dependencies": {
"foo": "1.0.0", // Version 1.0.0 exactly
"bar": ">1.0.0", // Any superior to 1.0.0
"baz": "1.2.x", // Any 1.2 version
"boo": "owner/repo", // The github project repo from owner
"asd": "[git url]", // The module in given repo
"local": "file:/path/to/file",
"tar": "http://web.site/my.tar.gz"
}}
```
* devDependencies: test runners, doc framework, ...
[package.json doc](https://docs.npmjs.com/files/package.json)
</section>
<section data-markdown>
## Versioning with Semver
* [Semantic Versionning](https://semver.org/)
* Version X.Y.Z
- X = Major, breaking changes
- Y = Minor, new features and backward compatible
- Z = Patch, bug fixing
* Version 0.Y.Z = unitial dev, unstable
</section>
<section data-markdown>
## Deterministic Installs: lock file
* `package-lock.json` for NPM
* `yarn.lock` for yarn
* Fixes dependency versions
* Avoids the "but it works on my computer!" situation
</section>
<section data-markdown>
## Nodemon
</section>
<section data-markdown>
## What is it ?
* A simple utility
* Watches your development files
* Restarts the server on saving
</section>
<section data-markdown>
## How to use it ?
```shell
npm i --save nodemon
# or
yarn add nodemon
# then
./node_modules/.bin/nodemon main.js
```
Or add this to package.json:
```json
"scripts": {
"dev": "./node_modules/.bin/nodemon "
}
```
and then:
```shell
npm run dev app.js
```
</section>
<section data-markdown>
## Presenting your work: `readme.md`
* Written in Markdown
* Should contain:
* Short introduction
* Installation instructions
* Usage instruction with simple (and advanced) examples
* List of contributors
[Markdown documentation](https://daringfireball.net/projects/markdown/syntax)
[How to write README](https://dev.to/scottydocs/how-to-write-a-kickass-readme-5af9)
</section>
<section data-markdown>
## Questions ?
</section>
<section data-markdown>
## Work
* You should have an `index.js` file with the server creation and `handles.js` defining the server's callback
* Add a `package.json` file with you module declaration
* Add a `readme.md` file with title, introduction, run instructions and your name
* Push it all to a GitLab / GitHub repository and send a link to your repository to [email protected]
</section>
</div>
</div>
<script src="../reveal.js/lib/js/head.min.js"></script>
<script src="../reveal.js/js/reveal.js"></script>
<script>
// Full list of configuration options available at:
// https://github.com/hakimel/../reveal.js#configuration
Reveal.initialize({
controls: true,
progress: true,
history: true,
center: true,
transition: 'slide', // none/fade/slide/convex/concave/zoom
// Optional ../reveal.js plugins
dependencies: [
{ src: '../reveal.js/lib/js/classList.js', condition: function() { return !document.body.classList; } },
{ src: '../reveal.js/plugin/markdown/marked.js', condition: function() { return !!document.querySelector( '[data-markdown]' ); } },
{ src: '../reveal.js/plugin/markdown/markdown.js', condition: function() { return !!document.querySelector( '[data-markdown]' ); } },
{ src: '../reveal.js/plugin/highlight/highlight.js', async: true, callback: function() { hljs.initHighlightingOnLoad(); } },
{ src: '../reveal.js/plugin/zoom-js/zoom.js', async: true },
{ src: '../reveal.js/plugin/notes/notes.js', async: true }
]
});
</script>
</body>
</html>