node.js let’s talk about · node.js event loop 9 js event loop thread worker pool thread...

30
Let’s talk about Node.js

Upload: others

Post on 20-May-2020

15 views

Category:

Documents


0 download

TRANSCRIPT

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

Thanks!Nils Mehlhornfreelance software engineer

30

nils-mehlhorn.de @n_mehlhorn

@n_mehlhorn

www