1 of 15

INTRO TO�NODEJS

There’s no sleep() in Javascript.

2 of 15

01

03

02

04

TABLE OF CONTENTS

Overview

Assignment

Features of the topic

About the Topic

3 of 15

INTRODUCTION

Node.js is an open-source and cross-platform JavaScript runtime environment.

  • Runs the V8 JavaScript engine, the core of Google Chrome, outside of the browser.
  • Runs on a single thread.
  • Allows asynchronous I/O using libuv.
  • Can be used for creating simple scripts to building large scale apis.

4 of 15

NODEJS TIMELINE

The Big Fork

io.js is a major fork of Node.js, with the goal of introducing ES6 support and moving faster

  • npm hits version 1.0
  • Larger companies start adopting Node.js: LinkedIn, Uber, etc.
  • NodeJS first release
  • The first form of npm is created
  • The Node.js Foundation is born
  • IO.js is merged back into Node.js
  • npm introduces private modules
  • Node.js 4 (v1, 2 & 3 never released)

2009

2011

2014

2015

5 of 15

Javascript

Single Threaded

Huge Ecosystem

Use the newest ECMAScript standards

NodeJS

ADVANTAGES

Why should I use NodeJS?

Allows developers to use the same language in both the front end and the back end

Allows handling thousands of concurrent connections without the burden of managing thread concurrency, I/O Callbacks

npm has more than 1.3 million packages

You are in charge of deciding which ECMAScript version to use by the installed nodejs version (instead of polyfilling like in the browser)

6 of 15

NODEJS vs The Browser

NodeJS

Browser

APIs

Filesystem, standard library

DOM, Platform APIs

Modules

CommonJS, ESM in modules

Bundlers, ESM in modules

Control over the environment

Yes

No, the user’s browser is the env

Global scope

global

winodw

7 of 15

CommonJS

ES Modules(node 13+)

Modules

math.js:

module.exports = {

add: (a, b) => a + b

}

index.js

const math = require(‘./math’);

const ten = math.add(6, 4);

math.js:

export const add = (a, b) => a + b;�

export default { add };

index.js

import math, { add } from ‘./math’;

const ten = math.add(6, 4);

const eleven = add(7, 4);

// in an async function

const dynamicMath = await import(‘./math’);

.mjs or type: ‘module’

8 of 15

NATIVE MODULES

Node-gyp is a tool compiling native c++ addon modules for Node.js

Instead of compiling we can download prebuilt binaries for our OS

9 of 15

NODE REPL (Interactive shell)

node command in the terminal - like F12 in the browser

10 of 15

Building a web server

http module

ExpressJS

Hapi

Fastify

11 of 15

Building a web server

USING THE HTTP MODULE

http module

ExpressJS

Hapi

Fastify

12 of 15

Building a web server

USING ExpressJS

http module

ExpressJS

Hapi

Fastify

13 of 15

EVENT DRIVEN PROGRAMMING

14 of 15

EXERCISE

We’ll build a simple nodejs app capable of converting strings to and from base64

  1. Create an empty directory with an index.js file.
  2. Run the file using the command “node index.js”.
  3. The app should accept two arguments (using argv), one for the ‘to’ or ‘from’ string and an encoded or decoded base64 string.
  4. Decoding/Encoding functions -
  5. To base64 - Buffer.from("Hello World").toString('base64')
  6. From base64 - Buffer.from("SGVsbG8gV29ybGQ=", 'base64').toString('ascii')
  7. The app should output the result for the encoding/decoding operation.
  8. Examples:
  9. node index.js to secret -> should output ‘c2VjcmV0'
  10. node index.js from c2VjcmV0 -> should output ‘secret’
  11. Bonus: move the conversion functions to a base64Utils.js module and require it from index.js

15 of 15

Thanks for listening!

Do you have any questions?