You are on page 1of 13

NODE JS

History of web browsers:


1990: Nexus → First web browser called WorldWideWeb
renamed to Nexus

1992: Lynx → text-based browser

1993: Mosaic → First web browser to allow images


embedded in text, making it world’s first most popular web
browser

1994: Netscape Navigator →Mosaic got updated with a new


name Netscape Navigator

1995: Netscape (Brendan Eich created Mocha in 10 days).


created Mocha. Then they changed its name to LiveScript.
Then the name JAVASCRIPT was given for marketing purpose
as back then, JAVA was the hot new language

Internet Explorer →Microsoft debuted their web browser

Microsoft realized that JAVASCRIPT changed the user


experience of the web and wanted the same for Internet
Explorer

1996: Microsoft created their own scripting language called


JScript.
▪ Due to differences in JScript and JAVASCRIPT it was difficult for
the developers to make their websites to work well in both
browsers.

▪ They started giving badges to their website “Best viewed in


Netscape” and “Best viewed in Internet Explorer.”

▪ Netscape wanted a standard specification that all browsers


could conform to as it would help keep other implementations
consistent across browsers.

▪ For these reasons Netscape submitted JAVASCRIPT to Echma


International.

▪ Echma International → An organization that creates standards


for technologies.

▪ Echma decided to use the term Echma Script as oracle owns


the trademark for JAVASCRIPT.

▪ Echma Script made its first appearance in Navigator 2.0


browser.

▪ ES6 came with the major update in the year 2015 which is also
termed as advanced JavaScript.
▪ TC39 (JavaScript board members) decided to bring update to
JS every year hence we also call the ES version by year.

▪ ES6: ES2015, ES7: ES2016, ES8:2017 etc.

▪ JavaScript is backward compatible: We can still use the older


versions of JavaScript to write our code.

▪ JavaScript is not forward compatible: The newer JavaScript


versions will not be compatible in the older browser. Hence
keep your browser updated. (If you still want to do this use
Babel. (Babel is a transpiler).

Compiler: A software that converts high-level language to low-


level assembly language.

Transpiler: Also called source-to-source compiler. A software


that translates from one high-level language to another high-
level language (Eg: Java to C++) or from Advanced version of a
language to an older version of the same language (ES6 to
ES5).
What is Node JS?
Client requests for the website

Server returns the response


Client Server
HTML CSS JS

Node JS is neither a library nor a framework it is a


JAVASCRIPT runtime built using chrome’s V8 Engine that
makes it possible to use JAVASCRIPT outside the browser.
 It was created by Ryan Dahl in 2009.
 Node JS is free to install and an open-source server
environment. (Open-source: Any developer can contribute
their work by sharing to the Node JS repository)
 It allows us to run JAVASCRIPT on the server.
 Node JS is cross platform i.e., it can run on multiple OS such
as Windows, Linux, Mac.
 Node JS comes with a lot of built-in modules.
Modules →Reusable piece of code/ code snippet that can be
used by importing the module in our code file.

JS Code → JS Engine →Machine Code


Machine code: The only language a computer can
understand comprising series of binary digits.

JS Engines of various browsers:


Edge: Chakra (Latest version of Edge uses V8)
Fire Fox: SpiderMonkey
Chrome: V8
Safari: JavaScriptCore
Node JS internally:
 Node.js has a collection of dependencies that it uses to
execute the code.

 Out of which two of the most important dependencies are
V8 and libuv.
 V8: Chrome V8 is a JAVASCRIPT Engine used in serverless
computing for executing serverless JAVASCRIPT i.e inside the
browser.
libuv: libuv is a multi-platform C library originally written for
Node JS to abstract non-blocking I/O operations based on
event loops.
 Node JS is a combination of both.
This schema explains how things really work:

 Some environment objects of browser are different from


that of Node JS.
Example: We cannot use DOM, window and all the other
objects that are provided by the browser.
 Also, browser does not provide us all the nice APIs that
Node JS provides through its modules.
Example: file system functionality (fs), creating servers (http)
etc.
Synchronous:
 Generally, JAVASCRIPT is synchronous i.e., the code runs in a
particular sequence of instructions.

Each instruction waits for its previous instruction to complete


its execution.

Example:
Code:
console.log(‘First’);
console.log(‘Second’);
console.log(‘Third’);

Output:
First
Second
Third

Asynchronous:
 Sometimes, important instructions get blocked due to some
previous instructions which causes delay in the user
interface.

Asynchronous code execution allows to execute next


instruction immediately and does not block the flow because
of previous instructions.
Example:
Code:
console.log(‘First’);
setTimeout(()=>console.log(‘Second’),2000);
console.log(‘Third’); setTimeout() is an asynchronous
function. The function defined
inside it is called a callback function.
Output: A callback is a function passed as an

First argument to another function.

Third
Second

JAVASCRIPT RUNTIME ENVIRONMENT:


Event/Task/Callback queue is all the If the stack is empty event loop
call back functions that are ready to takes the first thing on the
get fired. They wait for the call stack queue and pushes it on to the
to get empty. stack.

Server:
Stores all data associated with the websites that are hosted
by it. Processes client requests and provide appropriate
response.
 Uses HTTP (Hypertext Transfer Protocol) & other protocols
to respond to client request made over www.
 SMTP (Simple Mail Transfer Protocol) used for email transfer
and storage.
 FTP (File Transfer Protocol) used for file transfer and storage.
Displays website content through storing, processing, and
delivering webpages to users.

Single threaded process:


 Contains execution of instructions in a single sequence.
 One command process at a time.
 Only one thread handles the requests
SINGLE THREADED SERVER

Queue of Requests Response


thread

Multi-threaded process:
 Executes multiple instructions at a time.
MULTI-THREADED SERVER

thread thread
Queue of Requests Response
thread

A single thread can also execute multiple requests using I/O


non-blocking model.

Talking to database,
downloading stuff
Active time → Needs attention by thread
Inactive time →Does not need attention by thread

 So, if a server receives two requests req1 and req2, it


implements the CPU driven tasks of req1 and when req1
starts doing I/O driven tasks the thread gives attention to
req2 and implements its CPU driven tasks.

 Hence, when a request is doing I/O, the thread is not stuck


or blocked waiting for the request to finish. Hence it is called
non-blocking I/O.

 Non-blocking I/O allows a thread to suspend a request


temporarily while it is performing I/O to work on a different
request.

When the thread is stuck or blocked waiting for the I/O to


finish before it can go to the next process it is called Blocking
I/O.

Disadvantages of non-blocking I/O:


Only effective for I/O heavy workloads,
where I/O work >> CPU work. (In this case, efficiency gain is
higher)
 If CPU work >> I/O work, non-blocking I/O is not effective.
Why is node JS single threaded?
 Node JS was created as an experiment for asynchronous
processing.
 Synchronous processing is not feasible when there are large
number of clients with their requests.
So, it requires large number of threads or multiple servers to
handle numerous requests.
 Asynchronous processing on a single thread could provide
better performance and scalability than thread-based
implementation when the application is not doing any CPU
intensive work.
 Asynchronous processing on a single thread could provide
better performance and scalability than thread-based
implementation when the application is not doing any CPU
intensive work.

Where is Node JS used?


 I/O bound applications
 Data streaming applications
 Real time applications
 JSON APIs based applications

Basic features of Node JS:


 Read and write files on computer
 Connect to database
 Acts as a server for content
→ Install Node JS: https://nodejs.org/en/download/
→ Check Node JS version using terminal/Command Prompt:
> node OR node --v
→ REFER: Desktop/mynode

REPL:
REPL is used for experimenting with Node JS codes and
debugs JS codes inside the terminal.
Debugs → To detect and remove defects or errors

R: Read (Reads user input → Parses the input in JS data


structure → stores in memory)
Parse → converting data from one format to another

E: Eval (Evaluates the commands)

P: Print (Prints the result)

L: Goes back to the start to read the next input

Node modules:
1. Built-in node modules
2. Local modules
3. Third-party modules

You might also like