Node.js handbook

Presented by Arnaud Buchholz
Inspired from The definitive Node.js handbook from Flavio Copes
Presentation made with Reveal.js

Agenda

    Introduction to Node.js

    Overview

    • Node.js is a runtime environment for JavaScript that runs on the server
    • GitHub repository has more than 58k stars
    • Node.js is built on top of the Google Chrome V8 JavaScript engine

    It's fast, simple & asynchronous

    JavaScript allows to create asynchronous and non-blocking code in a very simple way, by using a single thread, callback functions and event-driven programming.

    When Node.js performs an I/O operation, instead of blocking the thread it will simply resume the operations when the response comes back.

    It's powerful

    This allows Node.js to handle thousands of concurrent connections with a single server without introducing the burden of managing threads concurrency.

    A vast ecosystem

    With its simple structure, the node package manager (npm) helped the ecosystem of Node.js proliferate.

    Now the npm registry hosts almost 500,000 open source packages you can freely use.

    ...or learn from.

    Hello World

    History 1/2

    2009 2010 2011 ...2015 2016
    Node.js
    npm
    Express
    Socket.io
    LinkedIn, Uber
    Hapi
    Node 4
    Foundation
    ES6 support
    Node 6
    Leftpad 🐛
    Yarn

    History 2/2

    2017 2018
    Node 8
    HTTP/2
    Security focus
    3 billion npm downloads/weeks
    Node 10
    ES Modules

    Why is it named Node.js ?

    Node is a single-threaded, single-process system which enforces shared-nothing design with OS process boundaries. It has rather good libraries for networking. I believe this to be a basis for designing very large distributed programs. The "nodes" need to be organized: given a communication protocol, told how to connect to each other.
    Ryan Dahl

    Node.js & ES6 Basics

    Installation

    • Download
    • Windows, macOS, Linux or even source code
    • Choose between LTS or Latest

    Running

    • In a command line prompt
    • Execute any script:
      node script.js
    • Or use the interactive Read-Eval-Print-Loop:
      node
      It offers an autocompletion feature, try with global.

    JavaScript

    • Node.js supports the JavaScript you know
    • It also supports future evolutions / variants of the language

    ES6 JavaScript


    Top 10 ES6 Features Every Busy JavaScript Developer Must Know

    ES6 Samples

    training-nodejs-playground

    ES6 const

    • const is for constant assignment

    • const is not for constant value

    ES6 let

    • let introduces block scoping to JavaScript
    • let does not benefit from hoisting

    ES6 Arrow functions

    • Compact alternative for function
    • No own bindings (this, arguments...)
    • Not for constructors or methods
    • Ideal for callbacks

    ES6 Arrow functions

    ES6 Template Literals

    • Compact alternative for string with embedded values
    • Supports Multi-line

    ES6 Spread syntax

    • When used on the right of an assignment,
      ... expands an iterable object into a list of parameters or elements

    ES6 Spread syntax

    ES6 Destructuring Assignment

    • Assign values from arrays, or properties from objects, into distinct variables
    • When used on the left of an assignment,
      ... is a collect syntax*

    * Not the official name, also supported for rest parameters

    ES6 Destructuring Assignment

    ES6 Promises , async and await

    • async functions are asynchronous and implicitly returns a promise
    • The await operator is used to wait for a promise

    ES6 Promises , async and await

    Other ES6 features

    • Default parameters
    const multiply = (a, b = 1) => a * b
    console.log(multiply(5)) // 5
    • Classes
    class Rectangle extends Shape {
    	constructor (width, height, filled = false, color = "black") {
    		super(filled, color)
    		this.width = width
    		this.height = height
    	}
    
    	get area () {
    		return this.width * this.height
    	}
    }

    Other ES6 features

    • Modules: import
    • Generator function: function*

    Node.js as a Host

    • Node.js has its own environment
      • The global context object is global

    • Additional features are available with require

    Node.js as a Command Line

    • The current Node.js instance information is available through the process object
    • console APIs output on the command line

    Node.js as a Command Line

    Reading from the command line is not straightforward

    Exercise 1: Explorer

    Prerequesites

    training-nodejs-playground

    Use Clone or download button

    (If you don't have git, just download zip and unzip it in a folder)

    A server to scan any folder

    Error management

    File system API

    • Node.js is designed to run on any environment
    • Operating system operations are abstracted

    fs module

    Check Node.js documentation of fs.statSync

    Step 1: synchronous readdir

    Step 2: asynchronous readdir

    Step 3: asynchronous readdir with promises

    ;