1 Node JS Part 1
1 Node JS Part 1
1
Who am I?
2
Content
1. Overview of Agile Software Development Process and RAD Methodology
2. JS
3. MERN
4. Project Management
5. CMS
6. Agile Testing
3
Intended Learning Objectives
1. To provide a firm foundation on Agile Software Development Concepts and
Methodology to gain knowledge sufficient to undertake a moderately complex system
development project using Agile practices.
2. Understand the basic concept behind JS,MERN Stack & CMS and how to apply them.
3. Understand the basic concept behind Project Management & Agile Testing and how
to apply them.
4
SCS2208 Rapid Application Development
Node JS - Part 1
[email protected]
Slides adapted from Mr. Shavindra Wickramathilaka
5
ILOs
▷ Describe Node JS
▷ Create a simple application using Node JS
6
Recap on JS
▷ A lightweight programming language ("scripting language")
▷ It is used to make web pages interactive
▷ A JavaScript is usually embedded directly into HTML pages
▷ JavaScript is an interpreted language (means that scripts execute without preliminary
compilation)
▷ Client-Side Scripting
7
JavaScript Usage
JS Machine
JS Code Engine Code
8
JavaScript Engines used by
browsers
9
Node JS
Node.js is an open-source, cross-platform runtime environment used for development of
server-side web applications. Node.js applications are written in JavaScript and can be run
on a wide variety of operating systems.
10
What is Node.js?
▷ The modern web application has really come a long way over the years with the
introduction of many popular frameworks such as bootstrap, Angular JS, etc. All of
these frameworks are based on the popular JavaScript framework.
▷ But when it came to developing server based applications there was just kind of a void,
and this is where Node.js came into the picture.
▷ Node.js is also based on the JavaScript framework, but it is used for developing
server-based applications.
11
What is Node.js? cont.
▷ It is often used to build back-end services (e.g: API)
▷ Application Programming Interfaces: These are the services that power our client
applications
Web APP
Back-End
Service
Mobile
App
12
What is Node.js? cont.
▷ Node.js is a very powerful JavaScript-based platform built on Google Chrome's
JavaScript V8 Engine. It is used to develop I/O intensive web applications like video
streaming sites, single-page applications, and other web applications. Node.js is open
source, completely free, and used by thousands of developers around the world.
▷ Node.js is based on an event-driven architecture and a non-blocking Input/Output API
that is designed to optimize an application's throughput and scalability for real-time
web applications.
13
What is Node.js? cont.
▷ Node.js is a set of bindings to the V8 JavaScript VM
▷ Allows one to script programs that do I/O in JavaScript
▷ Focuses on performance
14
Node History
▷ Till 2009 JavaScripts code could be executed only inside browsers.
▷ In 2009 Ryan Dahl developed Node to execute JavaScript outside the Browsers by
embedding V8 engine in C++ code.
▷ Node provides an environment to execute. This environment is different from the
environment provided by the browsers.
15
What’s special about Node
▷ Great for prototyping and agile development
▷ Superfast and highly scalable
▷ Use JavaScript
▷ Cleaner and more consistent codebase
▷ Largest ecosystem for open-source library
16
Example
▷ Paypal rebuilt one of their java and spring based application using node
▷ Built twice as fast with fewer people
▷ 33% few lines of code
▷ 40% fewer files
▷ Doubled the number of requests per second
▷ 35% faster response time
17
What could you build with Node?
▷ WebSocket Server
○ Eg: Chat Server
▷ Fast file upload client
▷ Ad Server
▷ Any real time data apps
18
What Node.Js is NOT?
▷ A Web Framework
▷ For Beginners: It’s very low level
▷ Multi Threaded: You can think of it as a single threaded server
19
Confusions
▷ NODE is NOT a programming language
○ NOT similar to C# or Ruby
▷ NODE is NOT Framework
○ NOT similar to ASP.NET or Ruby
▷ It’s a runtime environment for executing JavaScript Code to develop server-side
applications
20
Why should you use Node.js?
▷ Over the years, most of the applications were based on a stateless request-response
framework.
▷ In these sort of applications, it is up to the developer to ensure the right code was put
in place to ensure the state of web session was maintained while the user was working
with the system.
▷ But with Node.js web applications, you can now work in real-time and have a 2-way
communication.
▷ The state is maintained, and the either the client or server can start the
communication.
21
Synchronous vs Asynchronous
▷ Synchronous
○ Code executing in sequence.
○ Each time a function is called, the program execution
waits until that function returns before continuing to the
next line of code.
▷ Asynchronous
○ Execution doesn’t run in the sequence it appears in the
code.
○ Program doesn’t wait for the task to complete and can
move on to the next task.
22
Blocking Code
Read file from Filesystem, set equal to “contents”
Print contents
Do Something else
23
Non-Blocking Code
[This is a Callback]
Read file from Filesystem
Do Something else
24
Many web applications have
similar code to the following
25
Blocking vs Non-Blocking
Blocking Non-Blocking
26
Blocking vs Non-Blocking
27
Good Software
▷ Should be able to do multi tasking
▷ Should be able to run other threads of execution while waiting
28
Let’s look at some examples:
Apache vs NGINX
29
Apache vs NGINX
30
Apache vs NGINX
The difference?
31
Code like this either blocks the entire process or implies multiple execution stacks.
32
Solution
This code allows the program to return to the event loop immediately.
No machinery required.
33
However, the use of event-loops, call backs and non-blocking I/O not highly used by many
programmers due to cultural and infrastructural reasons
34
We normally learn I/O in this
manner
We ask the user to enter the input remain until we retrieve it from the user.
This is fine if we are getting information from one user. However, this code can give
problems if we are getting information from thousands of users.
35
Better solution
However, code like this is rejected as too complicated. People say it is spaghetti code.
However, this is a better solution for the code on the previous slide.
36
Asynchronous
▷ JavaScript is asynchronous in nature and so is Node.
▷ Asynchronous programming is a design pattern which ensures the non-blocking code
execution.
▷ Asynchronous code executes without having any dependency and no order.
▷ This improves the system efficiency and throughput.
37
How Node Works?
Single Thread
EVENT Queue
REQUEST REQUEST
38
Call Stack
JavaScript is Single threaded programming language | Single Threaded Runtime
At the most basic level, a call stack is a data structure that uses the Last In, First Out (LIFO)
principle to temporarily store and manage function invocation (call).
39
Event-Loop
This is where Event loop comes in
However we can do more than one thing concurrently since the browser is more than a run
time
Browser consists of APIs which are effectively threads that you can make calls to – they act
as multiple threads
40
Event-Loop
“The event loop is what allows Node.js to perform non-blocking I/O operations — despite
the fact that JavaScript is single-threaded — by offloading operations to the system kernel
whenever possible.”
41
42
Further Reading
https://nodejs.org/en/docs/guides/event-loop-timers-and-nexttick/
43
Missing infrastructure
▷ So why isn’t everyone using event loops?
○ Single threaded event loops require I/O to be non-blocking
○ Some libraries do not have these facilities.
○ No closures or anonymous functions in C; makes callbacks difficult.
○ Database libraries (e.g. libmysql client) do not provide support for
asynchronous queries
○ Asynchronous DNS resolution not standard on most systems.
44
Too much infrastructure
▷ EventMachine, Twisted, AnyEvent provide very good event loop
platforms.
▷ Easy to create efficient servers.
▷ But users are confused how to combine with other available libraries.
▷ Users still require expert knowledge of event loops, non-blocking I/O.
45
JS
▷ Javascript designed specifically to be used with an event loop:
▷ Anonymous functions, closures.
▷ Only one callback at a time.
▷ I/O through DOM event callbacks.
▷ Hence, the culture of JavaScript is already geared towards evented
programming.
46
Node JS
“To provide a purely evented, non-blocking infrastructure to script highly
concurrent programs.”
-Ryan Dahl-
47
Node Design
▷ In order to receive information from disk, network, or another process
the developer must use a callback.
▷ Hence, no function should directly perform I/O.
48
Node Design
▷ Low-level.
▷ Stream everything; never force the buffering of data.
49
Node Design
▷ Internet is just not IP.
▷ Therefore, NODE has built-in support for the most important
protocols:
▷ TCP, DNS, HTTP etc.
50
Node Design
▷ Platform Independent
▷ The API should be both familiar to client-side JS programmers and old
school UNIX hackers.
51
Node Architecture
Node Standard
JavaScript Library
C Node Binding
Thread Event
V8 Loop
Pool
52
Node.js Special Features
▷ Asynchronous and Event Driven
○ All APIs of Node.js library are asynchronous. Hence, the request is received by Node for some
Input/Output operation, it will execute the operation in the background and continue with
processing other requests.
▷ Very Fast
○ Being built on Google Chrome's V8 JavaScript Engine, Node.js library is very fast in code
execution.
▷ Handling of concurrent requests
○ Another key functionality of Node is the ability to handle concurrent connections with a very
minimal overhead on a single process.
▷ No Buffering
○ Node.js applications never buffer any data. These applications simply output the data in chunks.
53
Node.js Special Features
▷ Node uses the V8 JavaScript Runtime engine
○ The one which is used by Google Chrome. Node has a wrapper over the JavaScript engine which makes the
runtime engine much faster and hence processing of requests within Node also become faster.
▷ Single Threaded but Highly Scalable
○ Node.js uses a single threaded model with event looping. Event mechanism helps the server to respond in a
non-blocking way and makes the server highly scalable as opposed to traditional servers which create
limited threads to handle requests. Node.js uses a single threaded program and the same program can
provide service to a much larger number of requests than traditional servers like Apache HTTP Server.
▷ The Node.js library uses JavaScript
○ This is another important aspect of development in Node.js. A major part of the development community
are already well versed in javascript, and hence, development in Node.js becomes easier for a developer
who knows javascript.
54
When to use Node.js
▷ I/O Applications (e.g.: Chat applications)
▷ Data Streaming applications: Another ideal scenario to use Node is for
multimedia streaming servers wherein clients have request's to pull
different multimedia contents from this server.
▷ Data Intensive Real-time Applications (DIRT): Fast and
high-performance servers that need to processes thousands of
requests at a time, then this is an ideal framework.
▷ Advertisement servers – Again here you could have thousands of
request to pull advertisements from the central server and Node.js can
be an ideal framework to handle this.
55
When not to use Node.js
▷ CPU intensive applications
▷ Node is structured to be single threaded. If any application is required
to carry out some long running calculations in the background. So if the
server is doing some calculation, it won't be able to process any other
requests.
56
How to download Node
https://nodejs.org/en/download/
57
58
First App
var http = require('http');
59
First App
▷ Save the above code as app.js
▷ Navigate to the folder using CLI
▷ Run the following command
○ node app.js
60
First App
▷ Open your browser and navigate to localhost:8080
61
“
Thank You
62