Node.js Academy

Core concepts 😎

#ege-academy-nodejs @rbung @yagong

REQUIRE AND MODULE

CODE EXAMPLE

#ege-academy-nodejs @rbung @yagong

package resolution

require('./bar') // this will load file ./bar.js

require('bar')
// this is looking module in node_modules
// first ./node_modules/bar
// then ../node_modules/bar
// then ../../node_modules/bar
// until /node_modules/bar

#ege-academy-nodejs @rbung @yagong

How JS handles concurrency

#ege-academy-nodejs @rbung @yagong

Stack and frame

  • Each function call creates a frame
  • A stack is a set of frames

#ege-academy-nodejs @rbung @yagong

EVENTLOOP JS

simple version

#ege-academy-nodejs @rbung @yagong

CODE EXAMPLE

#ege-academy-nodejs @rbung @yagong

EVENTLOOP NODE

#ege-academy-nodejs @rbung @yagong

Best practices under eventloop model

  • keep each stack execution rapid: no blocking synchronous function call
  • keep each stack small: use as async function when possible

#ege-academy-nodejs @rbung @yagong

Asynchronous in NodeJS

Callback, Promess, Async/Await

#ege-academy-nodejs @rbung @yagong

Code example

#ege-academy-nodejs @rbung @yagong

NodeJS Module

#ege-academy-nodejs @rbung @yagong

What is a module in NodeJS?

  • Consider modules to be the same as JavaScript libraries
  • A set of functions you want to include in your application
  • Each file .js is a module

#ege-academy-nodejs @rbung @yagong

How to use a Module

const http = require('http') // buildin module
const lib = require('./lib') // local module
const toto = require('./toto.json') // json file

#ege-academy-nodejs @rbung @yagong

How to write your own module

module.exports = function() { // export a default function
  // lib logic
}

module.exports = { // export several functions
  foo: function() {},
  bar: function() {},
}

#ege-academy-nodejs @rbung @yagong

Some core modules

fs, cpu, stream

#ege-academy-nodejs @rbung @yagong

File system: FS

const fs = require('fs')
const readFile = require('util').promisify(fs.readFile)

readFile(`${__dirname}/module-usage/toto.json`, 'utf-8')
  .then(data => console.log(data))
  .catch(err => console.error(err))

#ege-academy-nodejs @rbung @yagong

system stats: CPU

npx vtop

#ege-academy-nodejs @rbung @yagong

stream

#ege-academy-nodejs @rbung @yagong

What is it for ?

🚀 Handle large amounts data in a very efficient way 😎

 

#ege-academy-nodejs @rbung @yagong

4 types of Stream

  • Readable
  • Writable
  • Duplex
  • Transform

#ege-academy-nodejs @rbung @yagong

Some examples

// Read a big file and write the content into the http response

const fs = require('fs');
const server = require('http').createServer();

server.on('request', (req, res) => {
  const src = fs.createReadStream('./big.file');
  src.pipe(res);
});

server.listen(8000);


// Compress a big file

const gzip = require('zlib').createGzip();
const fs = require('fs');

const inp = fs.createReadStream('The.Matrix.1080p.mkv');
const out = fs.createWriteStream('The.Matrix.1080p.mkv.gz');

inp.pipe(gzip).pipe(out);


#ege-academy-nodejs @rbung @yagong

Pipe operator

Event emitters

readableSrc
  .pipe(transformStream1)
  .pipe(transformStream2)
  .pipe(finalWrtitableDest)
readable.on('data', (chunk) => {
  writable.write(chunk);
});

#ege-academy-nodejs @rbung @yagong

https://github.com/rbung/ExerciceTransactionAnalyser

#ege-academy-nodejs @rbung @yagong

Made with Slides.com