-
Notifications
You must be signed in to change notification settings - Fork 0
/
node-intro.html
592 lines (450 loc) · 11.7 KB
/
node-intro.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
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
<!DOCTYPE html>
<html>
<head>
<title>Node for Novices</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
<style type="text/css">
@import url(https://fonts.googleapis.com/css?family=Yanone+Kaffeesatz);
@import url(https://fonts.googleapis.com/css?family=Droid+Serif:400,700,400italic);
@import url(https://fonts.googleapis.com/css?family=Ubuntu+Mono:400,700,400italic);
body {
font-family: 'Droid Serif';
}
h1, h2, h3 {
font-family: 'Yanone Kaffeesatz';
font-weight: normal;
margin-top: 0.1em;
}
.remark-slide-content {
height: 100%;
}
.the-end {
height: 100%;
width: 100%;
display: flex;
justify-content: center;
align-items: center;
text-align: center;
font-family: 'Yanone Kaffeesatz';
font-size: 250%;
}
.remark-container {
background-color: #fff;
}
.remark-slide-scaler {
box-shadow: none;
-webkit-box-shadow: none;
width: 98vw !important;
height: 98vh !important;
top: 0 !important;
left: 0 !important;
transform: scale(1.00) !important;
margin: auto auto;
}
.remark-slide-content {
font-size: 175%;
padding: 1em 1em;
}
.columns {
display: flex;
height: 100vh;
}
.col-left {
flex: none;
width: 18%;
height: 100%;
margin-right: 2%;
border-right: thin solid #ccc;
}
.col-right {
flex: none;
width: 78%;
}
.remark-code, .remark-inline-code {
font-size: 110%;
line-height: 110%;
font-family: 'Ubuntu Mono';
}
.remark-code {
border: thin solid #999;
border-radius: 5px;
}
.remark-inline-code {
font-size: 95%;
padding: 0.3% 1%;
background-color: hsl(0, 0%, 95%);
}
p {
line-height: 150%;
}
li {
margin-top: 0.5em;
}
li li {
margin-top: 0.25em;
}
.remark-slide-content .columns .col-left p,
.remark-slide-content .columns .col-left h1,
.remark-slide-content .columns .col-left h2,
.remark-slide-content .columns .col-left h3 {
font-size: 3.25vw;
}
.remark-slide-content .columns .col-left .remark-code,
.remark-slide-content .columns .col-left .remark-inline-code {
font-size: 2.5vw;
}
.do-not-do {
color: red;
font-size: 135%;
margin: 1em;
padding: 0.5em;
border: solid thin #d1d1d1;
background-color: hsl(61, 61%, 86%);
}
.shrink {
font-size: 90%;
}
</style>
</head>
<body>
<textarea id="source">
class: center, middle
# Node for Novices
### Sean Duncan
### (MetaSean)
???
Notes for the _first_ slide!
---
# What is Node.js?
>"Node.js® is a JavaScript runtime built on Chrome's V8 JavaScript engine. Node.js uses an event-driven, non-blocking I/O model that makes it lightweight and efficient. Node.js' package ecosystem, npm, is the largest ecosystem of open source libraries in the world."
https://nodejs.org/en/
---
# No, really, what is Node.js?
A stand-alone runtime environment for JavaScript.
--
- _not_ for browsers
- _for_ servers, hardware, your own laptop, etc
- starts with the same JavaScript engine as Chrome (i.e. V8)
- includes additional libraries (e.g. file handling)
--
- has a great, open-source ecosystem of other modules
--
- for a novice ☝️ are a good set of features to initially focus on, _but yes_, Node.js also has other awesome features!
--
---
# How to play with Node.js tonight
_Note_:  I'll be demoing in CodeAnywhere tonight,
    **_but it's better to work locally if you can!_**
**If you already have node and an IDE or texteditor installed:**
1. Create a new directory for tonight's code along (e.g. 'purple-people-eater')
**If you do *_not_* already have node installed:**
1. Go sign up for an account at [codeanywhere.com](https://codeanywhere.com)
2. Create a new project (e.g. 'purple-people-eater')
3. Add a new Connection Wizard, specifically the Node.js stack on Ubuntu
---
# Quick CodeAnywhere Tour
- View 👉 Layout 👉 Columns:2
- View 👉 File Explorer (`⌥-X`; `option-X`)
- View 👉 MiniMap (uncheck)
- Terminal tab: right-click project name 👉 SSH Terminal
- User 👉 Dashboard 👉 Projects
- Distraction-free mode: `⌘-ctl-⇧-F`
- New file: `ctrl-N`
---
layout: false
.columns[.col-left[
## `npm init`
]
.col-right[
# Initialize a local repository
```bash
npm init
```
_or_
```bash
/* use this version tonight */
`npm init --yes`
```
- [Relevant field descriptions](https://docs.npmjs.com/files/package.json)
]]
---
layout: false
.columns[.col-left[
## `npm init`
## `package.json`
]
.col-right[
# `package.json`
This file contains relevant metadata about your project, including other projects —known as 'modules'— that your project depends upon.
]]
---
layout: false
.columns[.col-left[
## `npm init`
## `package.json`
## `node_modules`
]
.col-right[
# `node_modules`
The `node_modules` directory contains any other node modules that you indicated that your project depends upon.
]]
---
layout: false
.columns[
.col-left[
## `npm init`
## `package.json`
## `node_modules`
## `yarn`
]
.col-right[
# `yarn`
- `yarn` is itself another node package…
- which reimplements some `npm` functionality.
You can install Yarn via:
```bash
npm install yarn --global
```
]
]
---
layout: false
.columns[
.col-left[
## local install
]
.col-right[
# Local Installations
.do-not-do[**_DO NOT DO, JUST WATCH AND LISTEN!_**]
```bash
npm install learnyounode
```
- `npm install` installs one or more node modules
- in this case, it installs the `learnyounode` module
- if no module name is provided, but there is another file (we'll look at that file in a moment) which includes dependencies, then it will install those modules
]
]
---
layout: false
.columns[
.col-left[
## local install
## local bin
]
.col-right[
# Local `bin`
.do-not-do[**_DO NOT DO, JUST WATCH AND LISTEN!_**]
```bash
node node_modules/learnyounode/bin/learnyounode
```
- `node` runs the file listed immediately following it
- in this case the file includes a path from the current working directory and the filename, where:
- `node_modules/learnyounode/bin/` is the path
- `learnyounode` is the filename
(which does not have the usual `.js` extension)
]
]
---
layout: false
.columns[
.col-left[
## local install
## local bin
## REPL
]
.col-right[
# REPL
```bash
node
```
- running it without any paramaters switches your terminal / command line to a Node REPL (Read-eval-print loop)
- **_If you want to, follow-along with this part._**
- `node`
- you'll notice the terminal prompt changes
- `1+1` >>> `2`
- `^-C` (ctrl-C) - _twice_ to exit the REPL mode
]
]
---
layout: false
.columns[
.col-left[
## local install
## local bin
## REPL
## scripts
]
.col-right[
.shrink[
.shrink[
.shrink[
.shrink[
# Scripts
.do-not-do[**_DO NOT DO, JUST WATCH AND LISTEN!_**]
```javascript
/* package.json */
"scripts": {
"teachMeNode": "node node_modules/learnyounode/bin/learnyounode"
},
```
Run the package's script with either:
```bash
npm run teachMeNode
```
_OR_
```bash
yarn run teachMeNode
```
_OR_
```bash
yarn teachMeNode
```
]
]
]
]
]
]
---
layout: false
.columns[
.col-left[
## local install
## local bin
## REPL
## scripts
]
.col-right[
# Scripts
### Custom scripts names for that npm project
- require `run`, e.g. `npm run teachMeNode`
- if there are no other command line or terminal commands with the same name, `yarn` does not require the `run` parameter
### Standard scripts associated with any npm project
- e.g. `test`, `start`, `stop` don't require `run`, e.g. `npm test`
- https://docs.npmjs.com/misc/scripts for the full list
]
]
---
layout: false
.columns[
.col-left[
## local install
## local bin
## REPL
## scripts
## global install
]
.col-right[
# Global Installations
Go ahead and run this command now:
```bash
`npm install learnyounode --global`
```
- available from anywhere on your computer
- global packages _can_ set-up their own global command line/terminal scripts
- for example, you now have a `learnyounode` command available
]
]
---
# Node School
**"learnyounode"** is just one of many self-based [workshoppers](https://nodeschool.io/#workshopper-list) available at [nodeschool.io](https://nodeschool.io/#workshopper-list) to help you learn essential Node.js-related skills.
--
----
**_Once you have the `learnyounode` workshopper installed, start working through it by running:_**
```bash
`learnyounode`
```
---
# 01-hello-world.js
```javascript
console.log("HELLO WORLD")
```
- [`console.log`](https://nodejs.org/dist/latest-v8.x/docs/api/console.html#console_console_log_data_args)
---
# 02-baby-steps.js
```javascript
const [cmd, file, `...numberArray`] = `process.argv`
const sum = nums `=>` nums`.reduce`(
(reduction, current) =>
reduction + parseInt(current),
0
)
`console.info`( sum(numberArray) )
```
- `...numberArray`, i.e. [destructuring assignment]( https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Destructuring_assignment)
- [`process.argv`](https://nodejs.org/dist/latest-v8.x/docs/api/process.html#process_process_argv)
- `=>`, i.e. [arrow function expression](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Arrow_functions)
- [Array's `reduce()` function](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/Reduce)
- [`console.info`](https://nodejs.org/api/console.html#console_console_info_data_args)
---
# 03-my-first-io.js
```javascript
const fs = require('fs')
const fileName = process.argv[2]
const fileBuffer = fs.readFileSync(fileName)
const fileContents = fileBuffer.toString()
const linesInFile = fileContents.split('\n')
const newLineCount = linesInFile.length - 1
console.log(newLineCount)
```
- `fs`, i.e. [File System]( https://nodejs.org/dist/latest-v8.x/docs/api/fs.html)
- [`Buffer`](https://nodejs.org/dist/latest-v8.x/docs/api/buffer.html)
---
# 04-my-first-async-io.js
.shrink[
.shrink[
```javascript
const fs = require('fs')
const fileName = process.argv[2]
fs.readFile(fileName, countLines)
function countLines (err, data) {
if (err) { throw err }
const newLineCount = data.toString()
.split('\n')
.length - 1
console.log(newLineCount)
}
```
- [`throw`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/throw)
- [`fs.readFile`]( https://nodejs.org/dist/latest-v8.x/docs/api/fs.html#fs_fs_readfile_path_options_callback)
- [Art of Node: Callbacks](https://github.com/maxogden/art-of-node#callbacks)
]
]
---
# 07-http-client.js
.shrink[
```javascript
const http = require('http')
const handleResponse = response =>
response.setEncoding('utf8')
.on('data', console.log)
.on('error', console.error)
http.get(process.argv[2], handleResponse)
.on('error', console.error)
}
```
]
---
.the-end[
**Stay Curious**
**and**
**Keep Coding!**
]
</textarea>
<script src="out/remark.js" type="text/javascript">
</script>
<script type="text/javascript">
var slideshow = remark.create({
highlightLanguage: 'javascript',
highlightLines: true,
highlightSpans: true,
highlightStyle: 'atom-one-light',
slideNumberFormat: (current, total) => '',
navigation: {
scroll: false,
click: false,
}
});
</script>
</body>
</html>