Nagpal Thread Factory - cable ties, cotton yarn, embroidery thread and loop pin supplier
Node.js Let’s talk about · Node.js Event Loop 9 JS Event Loop Thread Worker Pool Thread...
Transcript of Node.js Let’s talk about · Node.js Event Loop 9 JS Event Loop Thread Worker Pool Thread...
![Page 1: Node.js Let’s talk about · Node.js Event Loop 9 JS Event Loop Thread Worker Pool Thread readFile(cb) register cb() done more: What the heck is the event loop anyway? Thread Thread](https://reader034.fdocuments.in/reader034/viewer/2022042220/5ec6070f05cd871bb730b5f7/html5/thumbnails/1.jpg)
Let’s talk about Node.js
![Page 2: Node.js Let’s talk about · Node.js Event Loop 9 JS Event Loop Thread Worker Pool Thread readFile(cb) register cb() done more: What the heck is the event loop anyway? Thread Thread](https://reader034.fdocuments.in/reader034/viewer/2022042220/5ec6070f05cd871bb730b5f7/html5/thumbnails/2.jpg)
Hello!Nils Mehlhornfreelance software engineer
2
nils-mehlhorn.dewww @n_mehlhorn
@n_mehlhorn
![Page 3: Node.js Let’s talk about · Node.js Event Loop 9 JS Event Loop Thread Worker Pool Thread readFile(cb) register cb() done more: What the heck is the event loop anyway? Thread Thread](https://reader034.fdocuments.in/reader034/viewer/2022042220/5ec6070f05cd871bb730b5f7/html5/thumbnails/3.jpg)
3
What’s Node.js?
Views & APIs
Real World
![Page 4: Node.js Let’s talk about · Node.js Event Loop 9 JS Event Loop Thread Worker Pool Thread readFile(cb) register cb() done more: What the heck is the event loop anyway? Thread Thread](https://reader034.fdocuments.in/reader034/viewer/2022042220/5ec6070f05cd871bb730b5f7/html5/thumbnails/4.jpg)
What’s Node.js Let’s look at the
basics
4
![Page 5: Node.js Let’s talk about · Node.js Event Loop 9 JS Event Loop Thread Worker Pool Thread readFile(cb) register cb() done more: What the heck is the event loop anyway? Thread Thread](https://reader034.fdocuments.in/reader034/viewer/2022042220/5ec6070f05cd871bb730b5f7/html5/thumbnails/5.jpg)
Node.js is ...
… a JavaScript runtime
… based on Chrome’s V8 engine
… designed for scalable network apps
… asynchronous event-driven ?
… using non-blocking IO ??
5
![Page 6: Node.js Let’s talk about · Node.js Event Loop 9 JS Event Loop Thread Worker Pool Thread readFile(cb) register cb() done more: What the heck is the event loop anyway? Thread Thread](https://reader034.fdocuments.in/reader034/viewer/2022042220/5ec6070f05cd871bb730b5f7/html5/thumbnails/6.jpg)
6
IO = Access to slow stuff
● network● file system● database
![Page 7: Node.js Let’s talk about · Node.js Event Loop 9 JS Event Loop Thread Worker Pool Thread readFile(cb) register cb() done more: What the heck is the event loop anyway? Thread Thread](https://reader034.fdocuments.in/reader034/viewer/2022042220/5ec6070f05cd871bb730b5f7/html5/thumbnails/7.jpg)
Blocking IO
7
const fs = require('fs')
const options = {encoding: 'utf-8'}// blockconst content = fs.readFileSync('./file.txt', options) console.log(content)console.log('(other things your app does)')
Synchronous Call(JavaScript is single-threaded)
![Page 8: Node.js Let’s talk about · Node.js Event Loop 9 JS Event Loop Thread Worker Pool Thread readFile(cb) register cb() done more: What the heck is the event loop anyway? Thread Thread](https://reader034.fdocuments.in/reader034/viewer/2022042220/5ec6070f05cd871bb730b5f7/html5/thumbnails/8.jpg)
8
Non-Blocking IO
const fs = require('fs')
const options = {encoding: 'utf-8'}// no block, but asyncfs.readFile('./file.txt', options, (err, content) => { console.log(content)})console.log('(other things your app does)')
Asynchronous Callback
![Page 9: Node.js Let’s talk about · Node.js Event Loop 9 JS Event Loop Thread Worker Pool Thread readFile(cb) register cb() done more: What the heck is the event loop anyway? Thread Thread](https://reader034.fdocuments.in/reader034/viewer/2022042220/5ec6070f05cd871bb730b5f7/html5/thumbnails/9.jpg)
Node.js Event Loop
9
JS
Event Loop
Thread
Worker Pool
Thread
readFile(cb) register
donecb()
more: What the heck is the event loop anyway?
Thread
Thread
![Page 10: Node.js Let’s talk about · Node.js Event Loop 9 JS Event Loop Thread Worker Pool Thread readFile(cb) register cb() done more: What the heck is the event loop anyway? Thread Thread](https://reader034.fdocuments.in/reader034/viewer/2022042220/5ec6070f05cd871bb730b5f7/html5/thumbnails/10.jpg)
Promises
10
const fs = require('fs')const options = {encoding: 'utf-8'}
function getJsonFile(path, callback) { fs.readFile(path, options, (err, content) => { if (err) { callback(err) } const json = JSON.parse(content) callback(null, json) })}
function getJsonFile(path) { return fs.promises.readFile(path, options) .then(content => JSON.parse(content))}
▣ no “callback hell”
▣ less error-prone
▣ chainable
![Page 11: Node.js Let’s talk about · Node.js Event Loop 9 JS Event Loop Thread Worker Pool Thread readFile(cb) register cb() done more: What the heck is the event loop anyway? Thread Thread](https://reader034.fdocuments.in/reader034/viewer/2022042220/5ec6070f05cd871bb730b5f7/html5/thumbnails/11.jpg)
Views & APIs Let’s build for
the web
11
![Page 12: Node.js Let’s talk about · Node.js Event Loop 9 JS Event Loop Thread Worker Pool Thread readFile(cb) register cb() done more: What the heck is the event loop anyway? Thread Thread](https://reader034.fdocuments.in/reader034/viewer/2022042220/5ec6070f05cd871bb730b5f7/html5/thumbnails/12.jpg)
Express
12
web service
database
file system
Node.js
browser
Request Handling
View Rendering
Authentication
Routing
Logging
Express
HTTP Client
SQL Client
fs
app
![Page 13: Node.js Let’s talk about · Node.js Event Loop 9 JS Event Loop Thread Worker Pool Thread readFile(cb) register cb() done more: What the heck is the event loop anyway? Thread Thread](https://reader034.fdocuments.in/reader034/viewer/2022042220/5ec6070f05cd871bb730b5f7/html5/thumbnails/13.jpg)
Hello World with Express13
const express = require('express')const app = express()
app.get('/', (req, res) => { res.send('Hello World')})
const port = 3000app.listen(port, () => { console.log('Server started')})
http://localhost:3000
![Page 14: Node.js Let’s talk about · Node.js Event Loop 9 JS Event Loop Thread Worker Pool Thread readFile(cb) register cb() done more: What the heck is the event loop anyway? Thread Thread](https://reader034.fdocuments.in/reader034/viewer/2022042220/5ec6070f05cd871bb730b5f7/html5/thumbnails/14.jpg)
Views
14
app.set('views', './views'))app.set('view engine', 'ejs')
app.get('/greet', (req, res) => { const data = { title: 'iJS', person: req.query.person } res.render('greet', data)})
<!DOCTYPE html><html> <head> <title><%= title %></title> </head> <body> <h1><%= title %></h1> <p>Hey there, <%= person %>!</p> </body></html>
/greet?person=Delilah
greet.e
js
![Page 15: Node.js Let’s talk about · Node.js Event Loop 9 JS Event Loop Thread Worker Pool Thread readFile(cb) register cb() done more: What the heck is the event loop anyway? Thread Thread](https://reader034.fdocuments.in/reader034/viewer/2022042220/5ec6070f05cd871bb730b5f7/html5/thumbnails/15.jpg)
Embedded JavaScript Templates15
<html><body>
<% include(../header, {title}) %>
<main>Hello</main>
<% include ../footer %>
</body></html>
Includes
<title><%= title %></title>
Output
<ul> <% users.forEach(user => { %> <li><%= user.name %></li> <% }) %></ul>
Control Flow
![Page 16: Node.js Let’s talk about · Node.js Event Loop 9 JS Event Loop Thread Worker Pool Thread readFile(cb) register cb() done more: What the heck is the event loop anyway? Thread Thread](https://reader034.fdocuments.in/reader034/viewer/2022042220/5ec6070f05cd871bb730b5f7/html5/thumbnails/16.jpg)
APIs
16
let todos = [ {id: 1, text: "Do laundry", done: true}, {id: 2, text: "Prepare talk", done: true}, {id: 3, text: "Go on vacation", done: false}]
app.get('/todos', (req, res) => { res.json(notes)})
[ { "id": 1, "text": "Do laundry", "done": true }, { "id": 2, "text": "Prepare talk", "done": true }, { "id": 3, "text": "Go on vacation", "done": false }]
/todos
![Page 17: Node.js Let’s talk about · Node.js Event Loop 9 JS Event Loop Thread Worker Pool Thread readFile(cb) register cb() done more: What the heck is the event loop anyway? Thread Thread](https://reader034.fdocuments.in/reader034/viewer/2022042220/5ec6070f05cd871bb730b5f7/html5/thumbnails/17.jpg)
Back-End API17 17
/todos
🗹 Do laundry
🗹 Prepare talk
☐ Go on vacation
Enter text ... Add
![Page 18: Node.js Let’s talk about · Node.js Event Loop 9 JS Event Loop Thread Worker Pool Thread readFile(cb) register cb() done more: What the heck is the event loop anyway? Thread Thread](https://reader034.fdocuments.in/reader034/viewer/2022042220/5ec6070f05cd871bb730b5f7/html5/thumbnails/18.jpg)
Back-End API18 18
let todos = [...]
app.patch('/todos/:id', (req, res) => { const id = req.params.id const todo = todos
.find(todo => todo.id === id) todo.done = !todo.done res.sendStatus(200)})
/todos
🗹 Do laundry
🗹 Prepare talk
☐ Go on vacation
Enter text ... Add
PATCH /todos/3
![Page 19: Node.js Let’s talk about · Node.js Event Loop 9 JS Event Loop Thread Worker Pool Thread readFile(cb) register cb() done more: What the heck is the event loop anyway? Thread Thread](https://reader034.fdocuments.in/reader034/viewer/2022042220/5ec6070f05cd871bb730b5f7/html5/thumbnails/19.jpg)
Back-End API19 19
let todos = [...]
// parse JSON from HTTP bodyapp.use(express.json())
app.post('/todos', (req, res) => { const todo = req.body todo.id = getNextId() todo.done = false todos.push(todo) res.json(todo)})
/todos
🗹 Do laundry
🗹 Prepare talk
🗹 Go on vacation
Try Node.js Add
POST /todos{“text”: “Try Node.js”}
![Page 20: Node.js Let’s talk about · Node.js Event Loop 9 JS Event Loop Thread Worker Pool Thread readFile(cb) register cb() done more: What the heck is the event loop anyway? Thread Thread](https://reader034.fdocuments.in/reader034/viewer/2022042220/5ec6070f05cd871bb730b5f7/html5/thumbnails/20.jpg)
Middleware
20
const express = require('express')const path = require('path')
// serve static files from ./publicapp.use(express.static(
path.join(__dirname, 'public')))
/* request handlers */
// handle unmapped routesapp.use((req, res) => { res.status(404).send('Not Found')})
▣ Parsing
▣ Routing
▣ Static Files
▣ Error Handling
▣ Authentication
▣ Logging
order
![Page 21: Node.js Let’s talk about · Node.js Event Loop 9 JS Event Loop Thread Worker Pool Thread readFile(cb) register cb() done more: What the heck is the event loop anyway? Thread Thread](https://reader034.fdocuments.in/reader034/viewer/2022042220/5ec6070f05cd871bb730b5f7/html5/thumbnails/21.jpg)
Project Structure
21
// install generatornpm -g install express-generator
// generate projectexpress --view=ejs my-app
Configures:▣ Error handling▣ View rendering▣ Static files▣ Cookie parsing▣ Logging
![Page 22: Node.js Let’s talk about · Node.js Event Loop 9 JS Event Loop Thread Worker Pool Thread readFile(cb) register cb() done more: What the heck is the event loop anyway? Thread Thread](https://reader034.fdocuments.in/reader034/viewer/2022042220/5ec6070f05cd871bb730b5f7/html5/thumbnails/22.jpg)
package.json
22
{ "name": "ijs-express", "version": "0.0.1", "scripts": { "start": "node index.js",
"watch": "nodemon index.js", "start-ts: "ts-node index.ts" }, "dependencies": {...}, "devDependencies": { "nodemon": "^1.19.4", "ts-node": "^8.4.1" }}
![Page 23: Node.js Let’s talk about · Node.js Event Loop 9 JS Event Loop Thread Worker Pool Thread readFile(cb) register cb() done more: What the heck is the event loop anyway? Thread Thread](https://reader034.fdocuments.in/reader034/viewer/2022042220/5ec6070f05cd871bb730b5f7/html5/thumbnails/23.jpg)
Real World Node.js is a tool
23
![Page 24: Node.js Let’s talk about · Node.js Event Loop 9 JS Event Loop Thread Worker Pool Thread readFile(cb) register cb() done more: What the heck is the event loop anyway? Thread Thread](https://reader034.fdocuments.in/reader034/viewer/2022042220/5ec6070f05cd871bb730b5f7/html5/thumbnails/24.jpg)
Opportunities
24
Ecosystem
Full Stack JS
IO Performance
npm is biggest registry deployment options
tool support community
code sharing
no context switchhiring
onboarding
multitude of requests
connect downstream
services
less concurrency concerns
![Page 25: Node.js Let’s talk about · Node.js Event Loop 9 JS Event Loop Thread Worker Pool Thread readFile(cb) register cb() done more: What the heck is the event loop anyway? Thread Thread](https://reader034.fdocuments.in/reader034/viewer/2022042220/5ec6070f05cd871bb730b5f7/html5/thumbnails/25.jpg)
Challengesno typing
25
Security
Maintainability
CPU Performance
package corruption code injection
architecture
testing
linting
audits
best practices
worker threads
NestJSTypeScript
Jest
![Page 26: Node.js Let’s talk about · Node.js Event Loop 9 JS Event Loop Thread Worker Pool Thread readFile(cb) register cb() done more: What the heck is the event loop anyway? Thread Thread](https://reader034.fdocuments.in/reader034/viewer/2022042220/5ec6070f05cd871bb730b5f7/html5/thumbnails/26.jpg)
Use Cases
26
Realtime
▣ Chat▣ Gaming
Data-intensive
▣ Internet of Things
▣ Streaming
Web
▣ Server-side pages
▣ APIs
You’re most effective with what you have - until you’re not
![Page 27: Node.js Let’s talk about · Node.js Event Loop 9 JS Event Loop Thread Worker Pool Thread readFile(cb) register cb() done more: What the heck is the event loop anyway? Thread Thread](https://reader034.fdocuments.in/reader034/viewer/2022042220/5ec6070f05cd871bb730b5f7/html5/thumbnails/27.jpg)
27scenelab.io
![Page 28: Node.js Let’s talk about · Node.js Event Loop 9 JS Event Loop Thread Worker Pool Thread readFile(cb) register cb() done more: What the heck is the event loop anyway? Thread Thread](https://reader034.fdocuments.in/reader034/viewer/2022042220/5ec6070f05cd871bb730b5f7/html5/thumbnails/28.jpg)
Server-side Image Rendering
28
Node.js
fs
Google Cloud SDK
libvipssharp
fastify
JSON
PNGJPG
Cloud Storage
Raw Images
C++ Image Processing
![Page 29: Node.js Let’s talk about · Node.js Event Loop 9 JS Event Loop Thread Worker Pool Thread readFile(cb) register cb() done more: What the heck is the event loop anyway? Thread Thread](https://reader034.fdocuments.in/reader034/viewer/2022042220/5ec6070f05cd871bb730b5f7/html5/thumbnails/29.jpg)
Thumbnail Generation
29
upload image
trigger
Cloud Storage
Cloud Function
save thumbnail
![Page 30: Node.js Let’s talk about · Node.js Event Loop 9 JS Event Loop Thread Worker Pool Thread readFile(cb) register cb() done more: What the heck is the event loop anyway? Thread Thread](https://reader034.fdocuments.in/reader034/viewer/2022042220/5ec6070f05cd871bb730b5f7/html5/thumbnails/30.jpg)
Thanks!Nils Mehlhornfreelance software engineer
30
nils-mehlhorn.de @n_mehlhorn
@n_mehlhorn
www