NodeJS: Chapter 1
7 Nov 2020
Agenda
• Introduction
• Install nodejs
• Hello nodejs
• Nodejs module system
• Event loop
• Npm, yarn
• Create a project
• Frameworks
2
Introduction
• Open source, cross browser, runtime environment
• Executes javascript code on the server
• Build backend services
• Highy scalable, data intensive, and real time apps,
NOT for CPU intensive apps
3
Introduction
• Fewer lines, fewer files, fewer developers, more
requests, faster response
• JavaScript everywhere
• Clean, consistent, similar code on FE and BE
• Latest ECMAScript standards
• Many open source libraries
• Asynchronous I/O (unlike asp.net, ruby ...)
4
Introduction
• Who uses Node.js?
• GoDaddy, Groupon, IBM, LinkedIn, Microsoft,
Netflix, PayPal, SAP, Walmart, Yahoo, Amazon
Web Service
5
Introduction
• Browser = JS engine + browser code
• Nodejs = JS engine + C/C++ code
• Javasciprt = a programming language
• Nodejs = a runtime
• Express, Nest etc. = Nodejs Frameworks
6
Install nodejs
https://nodejs.org/en/
7
Hello nodejs
• Demo
8
Nodejs module system
• Module = sub program, packages as a unit
• CommonJS module system
• Each file is a separate module
• Use “require”
• Private & Public
• Module wrapper function
9
Event Loop
10
Call Stack
Event Queue
Some APIs
Npm, Yarn
• Download and manage dependencies
• Also used in frontend javascript projects
• Yarn: an alternative to npm
• Yet another resource negotiator
• Npm: after few years on Node.js
• Yarn (by facebook): to fix performance and security
concerns of npm
• yarn licenses list
• Yarn why package-name
11
Create a project
• Demo
12
Node.js frameworks
• https://nodejs.dev/learn#nodejs-frameworks-and-
tools
13
Next Topics
• Asynchronous Programming
• Some essential core modules
• Testcases in NodeJS
14
Asynchronous Programming
• JavaScript is synchronous by default and is single
threaded.
• Computers are asynchronous .
• Multiple process could be executed at the same
time.
• Normally, programming languages are
synchronous, and some provide a way to manage
asynchronicity, in the language or through
libraries.
15
Asynchronous Programming
• Node.js introduced a non-blocking I/O
environment to extend this concept to file access,
network calls and so on
• There are different ways to achieve asynchronous
programming in node js.
16
Different ways of Asynchronous
Programming
Need of asynchronous -:
Different I/O operations(File read/write,DB
operations,making http calls)
• Callbacks
• Promises
• Async-await
17
Callbacks
A callback is a simple function that's passed as a
value to another function and will only be executed
when the event happens.
(Events could be button click, api call, customized
event)
Demo-
18
Callbacks
• Callbacks are great for simple cases!
• However every callback adds a level of nesting,
and when you have lots of callbacks, the code
starts to be complicated very quickly.
19
Promises
A promise is an object that may produce a single
value some time in the future: either a resolved
value, or a reason that it’s not resolved (e.g., a
network error occurred)
20
Promises
• Once a promise has been called, it will start in
pending state. This means that the caller function
continues the execution, while it waits for the
promise to do its own processing, and give the
caller function some feedback.
Demo-
21
Async-Await
• Promises were introduced to solve the famous
callback hell problem, but they introduced
complexity on their own, and syntax complexity
• They were good primitives around which a better
syntax could be exposed to the developers, so
when the time was right we got async functions.
• They make the code look like it's synchronous, but
it's asynchronous and non-blocking behind the
scenes.
22
Async-Await
Async-
It simply allows us to write promises based code as
if it was synchronous and it checks that we are not
breaking the execution thread. It operates
asynchronously via the event-loop. Async functions
will always return a value. It makes sure that a
promise is returned and if it is not returned then
javascript automatically wraps it in a promise which
is resolved with its value.
23
Some essential core modules
While numerous libraries are available for extending
Node's functionalities, the engine comes with a set
of core modules implementing basic functionalities.
There's currently 34 core modules included in Node.
fs,path,os,events,http
24
fs
The fs module provides a lot of very useful
functionality to access and interact with the file
system.
All methods in this module are async in nature but
we can get sync as well by adding sync.
25
event,http
• The events module provides us the EventEmitter
class, which is key to working with events in Node.
• The http module of Node.js provides useful
functions and classes to build an HTTP server
26
Writing testcases in NodeJS
• Testing is a key element to any application. For
Node.js, the framework available for Testing is
called Jasmine. In early 2000, there was a
framework for testing JavaScript applications
called JsUnit. Later this framework got upgraded
and is now known as Jasmine.
27
Coming Soon…….
• Web-Socket in NodeJS
• Worker Threads
• Cluster mode
28

Node.js Chapter1

  • 1.
  • 2.
    Agenda • Introduction • Installnodejs • Hello nodejs • Nodejs module system • Event loop • Npm, yarn • Create a project • Frameworks 2
  • 3.
    Introduction • Open source,cross browser, runtime environment • Executes javascript code on the server • Build backend services • Highy scalable, data intensive, and real time apps, NOT for CPU intensive apps 3
  • 4.
    Introduction • Fewer lines,fewer files, fewer developers, more requests, faster response • JavaScript everywhere • Clean, consistent, similar code on FE and BE • Latest ECMAScript standards • Many open source libraries • Asynchronous I/O (unlike asp.net, ruby ...) 4
  • 5.
    Introduction • Who usesNode.js? • GoDaddy, Groupon, IBM, LinkedIn, Microsoft, Netflix, PayPal, SAP, Walmart, Yahoo, Amazon Web Service 5
  • 6.
    Introduction • Browser =JS engine + browser code • Nodejs = JS engine + C/C++ code • Javasciprt = a programming language • Nodejs = a runtime • Express, Nest etc. = Nodejs Frameworks 6
  • 7.
  • 8.
  • 9.
    Nodejs module system •Module = sub program, packages as a unit • CommonJS module system • Each file is a separate module • Use “require” • Private & Public • Module wrapper function 9
  • 10.
  • 11.
    Npm, Yarn • Downloadand manage dependencies • Also used in frontend javascript projects • Yarn: an alternative to npm • Yet another resource negotiator • Npm: after few years on Node.js • Yarn (by facebook): to fix performance and security concerns of npm • yarn licenses list • Yarn why package-name 11
  • 12.
  • 13.
  • 14.
    Next Topics • AsynchronousProgramming • Some essential core modules • Testcases in NodeJS 14
  • 15.
    Asynchronous Programming • JavaScriptis synchronous by default and is single threaded. • Computers are asynchronous . • Multiple process could be executed at the same time. • Normally, programming languages are synchronous, and some provide a way to manage asynchronicity, in the language or through libraries. 15
  • 16.
    Asynchronous Programming • Node.jsintroduced a non-blocking I/O environment to extend this concept to file access, network calls and so on • There are different ways to achieve asynchronous programming in node js. 16
  • 17.
    Different ways ofAsynchronous Programming Need of asynchronous -: Different I/O operations(File read/write,DB operations,making http calls) • Callbacks • Promises • Async-await 17
  • 18.
    Callbacks A callback isa simple function that's passed as a value to another function and will only be executed when the event happens. (Events could be button click, api call, customized event) Demo- 18
  • 19.
    Callbacks • Callbacks aregreat for simple cases! • However every callback adds a level of nesting, and when you have lots of callbacks, the code starts to be complicated very quickly. 19
  • 20.
    Promises A promise isan object that may produce a single value some time in the future: either a resolved value, or a reason that it’s not resolved (e.g., a network error occurred) 20
  • 21.
    Promises • Once apromise has been called, it will start in pending state. This means that the caller function continues the execution, while it waits for the promise to do its own processing, and give the caller function some feedback. Demo- 21
  • 22.
    Async-Await • Promises wereintroduced to solve the famous callback hell problem, but they introduced complexity on their own, and syntax complexity • They were good primitives around which a better syntax could be exposed to the developers, so when the time was right we got async functions. • They make the code look like it's synchronous, but it's asynchronous and non-blocking behind the scenes. 22
  • 23.
    Async-Await Async- It simply allowsus to write promises based code as if it was synchronous and it checks that we are not breaking the execution thread. It operates asynchronously via the event-loop. Async functions will always return a value. It makes sure that a promise is returned and if it is not returned then javascript automatically wraps it in a promise which is resolved with its value. 23
  • 24.
    Some essential coremodules While numerous libraries are available for extending Node's functionalities, the engine comes with a set of core modules implementing basic functionalities. There's currently 34 core modules included in Node. fs,path,os,events,http 24
  • 25.
    fs The fs moduleprovides a lot of very useful functionality to access and interact with the file system. All methods in this module are async in nature but we can get sync as well by adding sync. 25
  • 26.
    event,http • The eventsmodule provides us the EventEmitter class, which is key to working with events in Node. • The http module of Node.js provides useful functions and classes to build an HTTP server 26
  • 27.
    Writing testcases inNodeJS • Testing is a key element to any application. For Node.js, the framework available for Testing is called Jasmine. In early 2000, there was a framework for testing JavaScript applications called JsUnit. Later this framework got upgraded and is now known as Jasmine. 27
  • 28.
    Coming Soon……. • Web-Socketin NodeJS • Worker Threads • Cluster mode 28