Chapter One Introduction Node Js
Chapter One Introduction Node Js
Ch01
Introduction to Node js
1
Chapter Outline
What is Node js?
History of Node js
Features of Node.js
3 3
Introduction To Node js
• Node.js It is a popular tool for almost any kind of
project!
• Node.js runs the JavaScript engine, the core of Google
Chrome, outside of the browser. This allows Node.js
to be very performing.
• Node.js also provides a rich library of various
JavaScript modules which simplifies the development
of web applications using Node.js to a great extent.
Node.js = Runtime Environment + JavaScript Library
4
History of Node
• Node. js was written initially by Ryan
Dahl in 2009, about thirteen years
after the introduction of the first
server-side JavaScript environment,
Netscape's LiveWire Pro Web. The
initial release supported only Linux
and Mac OS . Its development and
maintenance was led by Dahl and later
sponsored by Joyent.
• Written in: C++, JavaScript
5
Features of Node.js
8
Why Should You Use Node.js?
There are multiple reasons as to why you should use
Node.js. Some of the advantages of Node.js are listed
below:
Using Node.js enables complete JavaScript stack
creation to be coordinated, which guarantees the
program’s rate and efficiency.
It is a real masterpiece of Node.js’ programming world
Node.js because it encourages you to create
applications that could effortlessly evolve with your
organization. Node.js operates best in systems that use
the design of small services, where functionality and
versatility can be accomplished rapidly and effortlessly.
9
What Should You Knowing Node.js?
• Knowing JavaScript provides a strong beginning
with Node.js for a programmer. You must recognize
the concepts of back-end designing, but
understanding the coding language can simplify
stuff a lot.
• As an open-source task, Node.js promotes
cooperation and innovations targeted at developing
and adopting the software. It is the goal of its Core
for the constant growth and advancement of
Node.js.
• Node.js helps reduce your company’s expenses,
which is one of the main reasons to use Node.js.
10
Importance of Node Js
• The following diagram depicts some important
parts of Node.js which we will discuss in detail in
the subsequent chapters.
11
Advantage of Node js
12
Disadvantage of Node js
13
JS Vs Node
14
Who Uses Node.js?
• The companies uses node js are includes eBay,
General Electric, GoDaddy, Microsoft, PayPal, Uber,
Wikipins, Yahoo!, and Yammer to name a few.
• LinkedIn - LinkedIn is using Node.js to power their
Mobile Servers, which powers the
• iPhone, Android, and Mobile Web products.
• Projects, Applications, and Companies Using Node
15
Where to Use Node.js?
• Following are the areas where Node.js is proving
itself as a perfect technology partner.
• I/O bound Applications
• Data Streaming Applications
• Data Intensive Real-time Applications (DIRT)
• JSON APIs based Applications
• Single Page Applications
Where Not to Use Node.js? Open Discussion
• It is not advisable to use Node.js for CPU intensive
applications.
16
When to not use Node.js
• Node.js can be used for a lot of applications with
various purpose, the only scenario where it
• should not be used is if there are long processing
times which is required by the application.
• 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. As discussed above,
Node.js is best when processing needs less
dedicated CPU time.
17
Node.js - Environment Setup
19
Continue….
• Step 2) Double click on the downloaded .msi file to
start the installation. Click the Run button
• in the first screen to begin the installation.
20
How to create First project
• Node.js scripts are created with the js file extension.
Remember that Node.js is not a programming
language. All the code in this course is JavaScript
code, which is why the js extension is used ,You can
run a Node.js script using the node command. Open
up a new terminal window and navigate to the
directory where the script lives. From the terminal, for
short ctrl + ~
21
Node.js Command Line Options
• The term REPL stands for Read Eval Print and Loop. It specifies a computer
environment like a window console or a Unix/Linux shell where you can enter the
commands and the system responds with an output in an interactive mode.
• REPL Environment
• The Node.js or node come bundled with REPL environment. Each part of the
• Read: It reads user's input; parse the input into JavaScript data-structure and stores
in memory.
Commands Description
.load filename It is used to load file content in current node repl session.
Starting
• There can be console-based and web-based node.js
applications.
• console.log('Hello Node.js!');
27
Normal Functions and function
Expression
• A function declaration tells the compiler about a function's
name, return type, and parameters. A function definition
provides the actual body of the function
• A function expression is very similar to and has almost the
same syntax as a function declaration (see function
statement for details).
• The function in function declaration can be accessed before
and after the function definition. The function in function
declaration can be accessed only after the function
definition.
• Function declarations are hoisted while Function
expressions are not hoisted.
28
Continue..
• //normal function
• function greet(){
• console.log('assalamu alykum');
• }
greet();
//function expression
•
var sayby= function(){
• console.log('macasalaama');
• }
•
sayby();
29
Functions hoisted and not hoisted
• //normal function
• greet();
• function greet(){
• console.log('assalamu alykum');
• }
//function expression
• sayby();
• var sayby= function(){
• console.log('macasalaama');
• }
30
Continue….
• const test = (name) =>{
• console.log(`hello , ${name}`);
•}
test('ali');
• test('osman')
31
Global objects
• Node. js global objects are global in nature and they
are available in all modules. These objects are
modules, functions, strings etc. Some of these
objects aren't actually in the global scope but in the
module scope.
• A list of Node.js global objects are given below:
• dirname
• __filename
• Console
• Process
32
Global Objects
• Buffer
• setImmediate(callback[, arg][, ...])
• setInterval(callback, delay[, arg][, ...])
• setTimeout(callback, delay[, arg][, ...])
• clearImmediate(immediateObject)
• clearInterval(intervalObject)
• clearTimeout(timeoutObject)
33
Node.js __dirname
• Output
34
Node.js __filename
35
setTimeout() method
36
clearTimeout() method
• clearTimeout() method: The clearTimeout() method is used to
37
setInterval() method
• setInterval() The setInterval() function is
commonly used to set a delay for functions that are
executed again and again, such as animations.
• The setInterval() method helps us to repeatedly
execute a function after a fixed delay
38
Node.js Errors
• System errors
• User-specified errors
• Assertion errors
39
Node.js ReferenceError Example 1
• standard Node js error :
• // Throws with a ReferenceError because b is unde
fined
• try {
• const a = 1;
• const c = a + b;
• } catch (err) {
• console.log(err);
• }
40
RangeError
• A RangeError is thrown when trying to pass a
value as an argument to a function that does not
allow a range that includes the value.
• let a = [];
• a.length = a.length - 5;
• console.log(a);
41
TypeError
• TypeError indicates that a passed argument is not of the
appropriate type. This method is used liberally throughout the
built-in Node API modules, and should also be used within your
own custom code to perform type checking at the top of your
functions and methods.
• let num = 11;
• try{
• num.toUpperCase();
• }
• catch (err){
• console.log(err);
• }
42
END
43