0% found this document useful (0 votes)
11 views78 pages

BE6IT CH#4 3161611 AWP Introduction To Node JS IT GECBVN BharatVainsh-202324

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views78 pages

BE6IT CH#4 3161611 AWP Introduction To Node JS IT GECBVN BharatVainsh-202324

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 78

GUJARAT TECHNOLOGICAL UNIVERSITY

Chandkheda, Ahmedabad
Affiliated

GOVERNMENT ENGINEERING COLLEGE


BHAVNAGAR
DEPARTMENT OF INFORMATION TECHNOLOGY

Advanced Web Programming (3161611 )


B. E. III, Semester –VI (Information Technology)

CHAPTER-III
Introduction to NodeJS
Subject Coordinator
Prof.Bharat Vainsh
Academic year
2023-24
Content

•Setup Node JS Environment


•Package Manager
•Features
•Console Object
•Concept of Callbacks

Bharat Vainsh-IT-GEC Bavnagar 2


Node JS Basic

•Node.js is a server-side runtime


environment built on Google Chrome's
JavaScript Engine (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.
Bharat Vainsh-IT-GEC Bavnagar 3
Node JS Basic

• Node.js is a cross-platform (run on


Windows, Linux, Unix, macOS, and more),
open-source, back-end JavaScript runtime
environment, that executes JavaScript code
outside a web browser.
•Node.js is a platform built on Chrome's
JavaScript runtime for easily building fast and
scalable network applications.

Bharat Vainsh-IT-GEC Bavnagar 4


Node JS Basic

• Node.js uses an event-driven, non-blocking


I/O model that makes it lightweight and
efficient, perfect for data-intensive real-time
applications that run across distributed
devices.
•The OpenJS Foundation, facilitated by the
Linux Foundation's Collaborative Projects
program, now handles the Node.js
distributed development.
Bharat Vainsh-IT-GEC Bavnagar 5
Node JS Basic

Bharat Vainsh-IT-GEC Bavnagar 6


Node JS Basic

Bharat Vainsh-IT-GEC Bavnagar 7


Where to Use Node.js?

• I/O bound Applications


•Data Streaming Applications
•Data Intensive Real-time Applications
(DIRT)
•JSON APIs based Applications
•Single Page Applications

Bharat Vainsh-IT-GEC Bavnagar 8


Node JS Basic
• Node.js is primarily used to build network
programs such as Web servers.
•you can build different types of applications
such as command line applications, web
applications, real-time chat applications,
REST APIs etc.
•A number web frameworks can be used to
accelerate the development of applications.
Some of the popular frameworks are
Express.js, Feathers.js, Koa.js, Sails.js, Meteor,
and many others.
Bharat Vainsh-IT-GEC Bavnagar 9
Node JS Basic
•Number of IDEs such as Atom, JetBrains
WebStorm, NetBeans, and Visual Studio
Code support development of Node.js
applications. Cloud-hosting platforms like
Google Cloud Platform and AWS Elastic
Beanstalk can be used to host Node.js
applications.

Bharat Vainsh-IT-GEC Bavnagar 10


Features of Node.js
•Asynchronous and Event Driven − All APIs of
Node.js library are asynchronous, that is,
non-blocking. It essentially means a Node.js
based server never waits for an API to return
data.
•The server moves to the next API after calling
it and a notification mechanism of Events of
Node.js helps the server to get a response
from the previous API call.
•Very Fast − Being built on Google Chrome's
V8 JavaScript Engine, Node.js library is very
fast in code execution.
Bharat Vainsh-IT-GEC Bavnagar 11
Features of Node.js
•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.
Bharat Vainsh-IT-GEC Bavnagar 12
Features of Node.js
• Scalable—It provides vast scalability for
applications. Node.js, being single-threaded,
is capable of handling a huge number of
simultaneous connections with high
throughput.
•Strong backend—Node.js is written in C and
C++, which makes it speedy and adds
features like networking support.
•Multi-platform—Cross-platform support
allows you to create SaaS websites, desktop
apps, and even mobile apps, all using
Node.js.
Bharat Vainsh-IT-GEC Bavnagar 13
Features of Node.js
•No Buffering − Node.js applications never buffer
any data. These applications simply output the
data in chunks.
•License − Node.js is released under the MIT
license.
•Speed—Non-blocking thread execution makes
Node.js even faster and more efficient.
•Packages—A vast set of open-source Node.js
packages is available that can simplify your
work. There are more than one million packages
in the NPM ecosystem today.
Bharat Vainsh-IT-GEC Bavnagar 14
Features of Node.js
• Maintainable—Node.js is an easy choice for
developers since both the frontend and
backend can be managed with JavaScript as a
single language.

•Easy—Node.js is quite easy to start with. It’s


a go-to choice for web development
beginners. With a lot of tutorials and a large
community—getting started is very easy.

Bharat Vainsh-IT-GEC Bavnagar 15


Applications Of Node.js

Bharat Vainsh-IT-GEC Bavnagar 16


Node.js Architecture and How It Works

Bharat Vainsh-IT-GEC Bavnagar 17


Node.js Architecture and How It Works
Node.js works differently. Let’s take a look
at each step it goes through:
• Node.js maintains a limited thread pool
to serve requests.
• Whenever a request comes, Node.js
places it into a queue.
• Now, the single-threaded “Event loop”—
the core component—comes into the
picture. This event loop waits for
requests indefinitely.
Bharat Vainsh-IT-GEC Bavnagar 18
Node.js Architecture and How It Works

• When a request comes in, the loop picks


it up from the queue and checks whether
it requires a blocking input/output (I/O)
operation. If not, it processes the request
and sends a response.
• If the request has a blocking operation to
perform, the event loop assigns a thread
from the internal thread pool to process
the request.
Bharat Vainsh-IT-GEC Bavnagar 19
Node.js Architecture and How It Works

• There are limited internal threads


available. This group of auxiliary threads
is called the worker group.

• The event loop tracks blocking requests


and places them in the queue once the
blocking task is processed. This is how it
maintains its non-blocking nature.

Bharat Vainsh-IT-GEC Bavnagar 20


Popular Packages
• Here are some of the most popular packages for Node.js today:
• Express – Express.js, or simply Express, is a Sinatra-inspired web development framework
for Node.js, and the de-facto standard for the majority of Node.js applications out there
today.
• MongoDB – The official driver to MongoDB. It provides the API for MongoDB object
databases in Node.js.
• Socket.io – Socket enables real-time, bidirectional, and event-based communication.
• Lodash – Lodash makes JavaScript easier by taking the hassle out of working with arrays,
numbers, objects, strings, etc.
• Moment – A JavaScript date library for parsing, validating, manipulating, and formatting
dates.
• Commander.js – This is all you need to work and build with command-line interfaces for
node.js.
• Forever – A simple CLI tool for ensuring that a given script runs continuously (i.e. forever).
Keeps your Node.js process up in production in the face of any unexpected failures.
• Async – A utility module that provides straightforward, powerful functions for working with
asynchronous JavaScript.
• Redis – A client library for supporting Redis database integration.
• Mocha – A clean, flexible JavaScript test framework for Node.js and the browser.
• Passport – Simple, unobtrusive authentication for Node.js. Passport’s sole purpose is to
authenticate requests.

Bharat Vainsh-IT-GEC Bavnagar 21


Who uses Node.js
•Node.js is used by many large companies.
Below is a list of a few of them.
•Paypal – A lot of sites within Paypal have also
started the transition onto Node.js.
•LinkedIn – LinkedIn is using Node.js to power
their Mobile Servers, which powers the
iPhone, Android, and Mobile Web products.
•Mozilla has implemented Node.js to support
browser APIs which has half a billion installs.
•eBay hosts their HTTP API service in Node.js
Bharat Vainsh-IT-GEC Bavnagar 22
When to Use Node.js
• Node.js is best for usage in streaming or event-
based real-time applications like
• Chat applications
• Game servers – Fast and high-performance servers
that need to processes thousands of requests at a
time, then this is an ideal framework.
• Good for collaborative environment – This is good
for environments which manage documents. In a
document management environment, you will have
multiple people who post their documents and do
constant changes by checking out and checking in
documents.

Bharat Vainsh-IT-GEC Bavnagar 23


When to Use Node.js
•So Node.js is good for these environments
because the event loop in Node.js can be
triggered whenever documents are changed in a
document managed environment.
•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.
•Streaming servers – Another ideal scenario to
use Node is for multimedia streaming servers
wherein clients have request’s to pull different
multimedia contents from this server.

Bharat Vainsh-IT-GEC Bavnagar 24


When to not use Node.js
• Node.js can be used for a lot of applications with
various purposes. The only scenario where it should
not be used is where there are long processing
times, which is required by the application.

• Node is structured to be single-threaded. If an


application is required to carry out some long-
running calculations in the background, it won’t be
able to process any other requests. As discussed
above, Node.js is used best where processing needs
less dedicated CPU time.

Bharat Vainsh-IT-GEC Bavnagar 25


NPM - Node Package Manager

Bharat Vainsh-IT-GEC Bavnagar 26


NPM - Node Package Manager

•Node Package Manager (NPM) is a command


line tool that installs, updates or uninstalls
Node.js packages in your application.
•NPM manages all the packages and modules
for Node.js and consists of command line
client npm.
•A package contains all the files needed for a
module and modules are the JavaScript
libraries that can be included in Node project
according to the requirement of the project.

Bharat Vainsh-IT-GEC Bavnagar 27


NPM - Node Package Manager

•NPM can install all the dependencies of


a project through the package.json file.
It can also update and uninstall
packages.
•It also provides an online repository for
node.js packages/modules which are
searchable
on https://www.npmjs.com/.

Bharat Vainsh-IT-GEC Bavnagar 28


NPM - Node Package Manager

•verify NPM installation by writing the


following command in terminal or command
prompt.
•C:\> npm -v
2.11.3
To access NPM help, write npm help in the
command prompt or terminal window
C:\> npm help
Bharat Vainsh-IT-GEC Bavnagar 29
NPM - Node Package Manager
• If you have an older version of NPM then you can
update it to the latest version using the following
command.
C:\> npm install npm –g

Install Package Locally


•Use the following command to install any third
party module in your local Node.js project
folder.
C:\>npm install <package name>
Bharat Vainsh-IT-GEC Bavnagar 30
NPM - Node Package Manager

Add Dependency into package.json


Use --save at the end of the install command
to add dependency entry into package.json of
your application.
C:\MyNodeProj> npm install
express --save

Bharat Vainsh-IT-GEC Bavnagar 31


NPM - Node Package Manager

{
"name": "NodejsConsoleApp",
"version": "0.0.0",
"description": "NodejsConsoleApp",
"main": "app.js",
"author": {
"name": "Dev",
"email": ""
},
"dependencies": {
"express": "^4.13.3"
}
Bharat Vainsh-IT-GEC Bavnagar 32
}
NPM - Node Package Manager

Update Package
To update the package installed locally in your
Node.js project, navigate the command
prompt or terminal window path to the
project folder and write the following update
command.

C:\MyNodeProj> npm update


<package name>
Bharat Vainsh-IT-GEC Bavnagar 33
NPM - Node Package Manager
Uninstall Packages
Use the following command to remove a local
package from your project.
C:\>npm uninstall <package
name>
The following command will uninstall
ExpressJS from the application.
C:\MyNodeProj> npm uninstall
express
Bharat Vainsh-IT-GEC Bavnagar 34
Install Node.js

The following tools/SDK are required for


developing a Node.js application on any
platform.
•Node.js
•Node Package Manager (NPM)
•IDE (Integrated Development Environment)
or TextEditor

Bharat Vainsh-IT-GEC Bavnagar 35


Install Node.js

Install Node.js on Windows


Visit Node.js official web site
https://nodejs.org.
It will automatically detect OS and
display download link as per your
Operating System.

Bharat Vainsh-IT-GEC Bavnagar 36


Install Node.js

Bharat Vainsh-IT-GEC Bavnagar 37


Install Node.js

Bharat Vainsh-IT-GEC Bavnagar 38


Install Node.js

• Click Next to read and accept the License


Agreement and then click Install.
• It will install Node.js quickly on your
computer.
• Finally, click finish to complete the
installation.
Verify Installation
•Once you install Node.js on your computer,
you can verify it by opening the command
prompt and typing
node -v
Bharat Vainsh-IT-GEC Bavnagar 39
Install Node.js

Bharat Vainsh-IT-GEC Bavnagar 40


How To Write and Run Your First Program in
Node.js

Step 1 — Outputting to the Console

hello.js

console.log("Hello World");

Bharat Vainsh-IT-GEC Bavnagar 41


How To Write and Run Your First Program in
Node.js

Step 2 — Running the Program

node hello.js

Output
Hello World

Bharat Vainsh-IT-GEC Bavnagar 42


Node.js Basics

Primitive Types
Node.js includes following primitive types:Everything
else is an object in Node.js.
•String
•Number
•Boolean
•Undefined
•Null
•RegExp
Bharat Vainsh-IT-GEC Bavnagar 43
Node.js Basics

Loose Typing
• JavaScript in Node.js supports loose typing like the browser's
JavaScript. Use var keyword to declare a variable of any type.

Object Literal
var obj =
{
authorName: 'Ryan Dahl',
language: 'Node.js'
}
Bharat Vainsh-IT-GEC Bavnagar 44
Node.js Basics
Functions
• Functions are first class citizens in Node's JavaScript,
similar to the browser's JavaScript.
• A function can have attributes and properties also.
It can be treated like a class in JavaScript.

function Display(x)
{
console.log(x);
}
Display(100);
Bharat Vainsh-IT-GEC Bavnagar 45
Node.js Basics

Buffer
• Node.js includes an additional data type called Buffer (not
available in browser's JavaScript).
• Buffer is mainly used to store binary data, while reading
from a file or receiving packets over the network.

Process object
• Each Node.js script runs in a process.
• It includes process object to get all the information about
the current process of Node.js application.
Bharat Vainsh-IT-GEC Bavnagar 46
Node.js Modules

What is a Module in Node.js?


• Consider modules to be the same as JavaScript libraries.
• A set of functions you want to include in your
application.
• Module in Node.js is a simple or complex functionality
organized in single or multiple JavaScript files which can
be reused throughout the Node.js application.
• Each module in Node.js has its own context, so it cannot
interfere with other modules or pollute global scope.
Also, each module can be placed in a separate .js file
under a separate folder.

Bharat Vainsh-IT-GEC Bavnagar 47


Node.js Basics

Node.js Module Types


Node.js includes three types of modules:
• Core Modules
• Local Modules
• Third Party Modules
Built-in Modules
• Node.js has a set of built-in modules which you can
use without any further installation.

Bharat Vainsh-IT-GEC Bavnagar 48


Node.js Basics

Core Module Description


http http module includes classes,
methods and events to create
Node.js http server.
url url module includes methods for
URL resolution and parsing.
querystring querystring module
includes methods to deal with
query string.
Bharat Vainsh-IT-GEC Bavnagar 49
Node.js Basics

path path module includes


methods to deal with file paths.
fs fs module includes classes,
methods, and events to work with
file I/O.
util util module includes utility
functions useful for programmers.
Bharat Vainsh-IT-GEC Bavnagar 50
How To Write and Run Your First Program in
Node.js
var http = require('http');
http.createServer(function (req, res)
{
res.writeHead(200, {'Content-Type':
'text/html'});
//res.write('hi!first node js
programming')
res.end('Hello World!');
}).listen(8000);
Bharat Vainsh-IT-GEC Bavnagar 51
How To Write and Run Your First Program in
Node.js

Bharat Vainsh-IT-GEC Bavnagar 52


READ, EVALUATE, PRINT and LOOP(REPL)
The Node.js runtime has a built-in interactive shell, in which
you can execute instructions one at a time. The Node.js
interactive shell works on the principle of REPL, which is an
acronym for READ, EVALUATE, PRINT and LOOP.
• The Node.js interactive REPL terminal is like the Powershell
or Command prompt terminal, or a bash terminal in Linux. It
performs the following tasks −
• Read − Reads user's input, parses the input into JavaScript
data-structure, and stores in memory.
• Eval − Takes and evaluates the data structure.
• Print − Prints the result.
• Loop − The terminal is ready to receive next input from the
user.
Bharat Vainsh-IT-GEC Bavnagar 53
READ, EVALUATE, PRINT and LOOP(REPL)
To start the Node.js REPL on your computer, simply
enter node in the command terminal (without the
javascript file name as done before). The Node.js
prompt > will appear.
C:\nodejs>node
Welcome to Node.js v20.9.0.
Type ".help" for more information.
>
• The REPL feature of Node is very useful in
experimenting with Node.js codes and to debug
JavaScript codes.
Bharat Vainsh-IT-GEC Bavnagar 54
READ, EVALUATE, PRINT and LOOP(REPL)
> 10+20
30
> "Hello"+"World"
'HelloWorld'
> a=10
10
> b=20
20
> a+b
30
> Math.random()
0.5423940959293392
>
Bharat Vainsh-IT-GEC Bavnagar 55
READ, EVALUATE, PRINT and LOOP(REPL)
Multiline Expression
> x=0
0
> do {
... x++;
... console.log("x: "+x);
... }
... while (x<5);
x: 1
x: 2
x: 3
x: 4
x: 5
undefined
>

Bharat Vainsh-IT-GEC Bavnagar 56


READ, EVALUATE, PRINT and LOOP(REPL)
• Underscore Variable
• You can use underscore (_) to get the last result
> var x=10
undefined
> var y=20
undefined
> x+y
30
> var z= _
undefined
>z
30
>
Bharat Vainsh-IT-GEC Bavnagar 57
READ, EVALUATE, PRINT and LOOP(REPL)
Dot commands

1.help
shows the dot commands help

2.editor
enables editor mode, to write multiline JavaScript code with ease.
Once you are in this mode, enter ctrl-D to run the code you wrote.

3.break
when inputting a multi-line expression, entering the .break
command will abort further input. Same as pressing ctrl-C.

4.clear
resets the REPL context to an empty object and clears any multi-line
expression currently being input.

Bharat Vainsh-IT-GEC Bavnagar 58


READ, EVALUATE, PRINT and LOOP(REPL)
Dot commands

5.load
loads a JavaScript file, relative to the current working directory

6.save
saves all you entered in the REPL session to a file (specify the filename)

7.exit
exits the repl (same as pressing ctrl-C two times)

8 Up/Down Keys
see command history and modify previous commands.

9 tab Keys
list of current commands.

Bharat Vainsh-IT-GEC Bavnagar 59


READ, EVALUATE, PRINT and LOOP(REPL)
Evaluate script
PS D:\nodejs> node --eval "console.log(123)"
123
PS D:\nodejs> node -e "console.log(123)"
123

Display version
PS D:\nodejs> node -v
v20.9.0
PS D:\nodejs> node --version
v20.9.0
Bharat Vainsh-IT-GEC Bavnagar 60
READ, EVALUATE, PRINT and LOOP(REPL)
Show help
PS D:\nodejs> node -h
PS D:\nodejs> node –help

Start REPL
PS D:\nodejs> node -i
PS D:\nodejs> node –interactive

Load module
PS D:\nodejs> node -r "http"
PS D:\nodejs> node –require "http"
Bharat Vainsh-IT-GEC Bavnagar 61
READ, EVALUATE, PRINT and LOOP(REPL)
Show help
PS D:\nodejs> node -h
PS D:\nodejs> node –help

Start REPL
PS D:\nodejs> node -i
PS D:\nodejs> node –interactive

Load module
PS D:\nodejs> node -r "http"
PS D:\nodejs> node –require "http"
Bharat Vainsh-IT-GEC Bavnagar 62
Console Object

NodeJS - console.log() Method


The Node.js console.log() method is used to
print the given messages to the standard
output stream (stdout) ie. in the terminal or
other logging mechanisms, such as a file or
network connection.
It takes any number of arguments and prints
them out separated by spaces with a newline
at the end.
Bharat Vainsh-IT-GEC Bavnagar 63
Console Object

Syntax
Following is the syntax of the Node.js console.log()
method −
console.log([data] [, …args]);
Parameters
This function accepts two parameters and those are
demonstrated below.
data − This parameter specifies the message that will
be shown on the console.
args − This is an op onal parameter, it holds the
substitution values that will be passed to the data.
Bharat Vainsh-IT-GEC Bavnagar 64
Console Object
Return value
This method doesn't return anything; instead,
it prints the formatted message to stdout in a
new line on the console.
console.info() method of node.js will accept
an optional parameter (args).
•In this example, we are calling the
console.log() function with two parameters
data and args. We are passing string values as
substitution values.

Bharat Vainsh-IT-GEC Bavnagar 65


Console Object
Console class − Methods like console.log(),
console.warn(), and console.error are present
in the console class. Any Node.js stream can
be written using these methods.
Global console − The global console instance
is configured to
write process.stdout and process.stderr(this
property returns a stream connected to
stdout). It is possible to operate without using
the require("node:console") function.
Bharat Vainsh-IT-GEC Bavnagar 66
Console Object
List of Methods
Following are the list of methods available in the
console class
1 assert()
Used to verify the invariants.
2 clear()
Used to clear the stdout (standard output).
3 count()
Used to get the count of the number of times the
function has been called with a specific input value.
4 countReset()
Used to reset the count for the specific input value
which is passed as a label.
Bharat Vainsh-IT-GEC Bavnagar 67
Console Object
5 debug()
Used to print information to stdout in a new line
6 dir()
Used to get the properties of a particular object.
7 dirxml()
Used to call the console.log() method at the time
of execution by passing the arguments received.
8 error()
Used to display the error messages on the
console.

Bharat Vainsh-IT-GEC Bavnagar 68


Console Object
9 group()
Used to get the passed information in the method as a
grouped format.
10 groupCollapsed()
Used to collapse the previously created group by
console.group().
11 groupEnd()
Used to end the group; which was created with
console.group() and console.groupCollapsed() methods.
12 info()
Used to print information to stdout in a new line.
13 log()
Used to print information to stdout in a new line.

Bharat Vainsh-IT-GEC Bavnagar 69


Console Object
17 timeLog()
Used to print the elapsed time for a timer that
was previously started by calling console.time().
18 trace()
Used to print the stack 'Trace' to the current
position of the code followed by the message and
substitution values on stderr in a new line.
19 warn()
Used to print the warning messages to the
console.
Bharat Vainsh-IT-GEC Bavnagar 70
Console Object
14 table()
Used to create a table in the console.
15 time()
Used to start a timer that we can use to track the
time taken by an operation or any function.
16 timeEnd()
Used to stop the timer which was previously
started by console.time() method.
20 new Console()
creates a new Console with one or two instances
of writable streams, namely stdout and stderr.

Bharat Vainsh-IT-GEC Bavnagar 71


Concept of Callbacks
Node. js callbacks are a special type of function
passed as an argument to another function.
They're called when the function that contains
the callback as an argument completes its
execution, and allows the code in the callback to
run in the meantime.
Callbacks help us make asynchronous calls.
Example
setTimeout(function () { console.log('This prints
after 1000 ms'); }, 1000); console.log("Hello
World");
Bharat Vainsh-IT-GEC Bavnagar 72
Concept of Callbacks
Node. js callbacks are a special type of function
passed as an argument to another function.
They're called when the function that contains
the callback as an argument completes its
execution, and allows the code in the callback to
run in the meantime.
Callbacks help us make asynchronous calls.
Example
setTimeout(function () { console.log('This prints
after 1000 ms'); }, 1000); console.log("Hello
World");
Bharat Vainsh-IT-GEC Bavnagar 73
Concept of Callbacks
• A Callback in Node.js is an asynchronous equivalent
for a function.
• It is a special type of function passed as an argument
to another function. Node.js makes heavy use of
callbacks.
• Callbacks help us make asynchronous calls.
• All the APIs of Node are written in such a way that
they support callbacks.
• Programming instructions are executed synchronously
by default.
• If one of the instructions in a program is expected to
perform a lengthy process, the main thread of
execution gets blocked.

Bharat Vainsh-IT-GEC Bavnagar 74


Concept of Callbacks
•The subsequent instructions can be executed
only after the current I/O is complete.
• This is where callbacks come in to the
picture.
•The callback is called when the function that
contains the callback as an argument
completes its execution, and allows the code
in the callback to run in the meantime.
• This makes Node.js highly scalable, as it can
process a high number of requests without
waiting for any function to return results.
Bharat Vainsh-IT-GEC Bavnagar 75
Experiments

•How to create a simple server in Node.js


that returns Hello World?
•What is REPL? What purpose it is used
for?
•Define Node Package Manager (NPM).
Explain the process of installing,
upgrading and uninstalling module using
NPM.

Bharat Vainsh-IT-GEC Bavnagar 76


Experiments

•Write a Node JS code to create a server


that will write a log of visited pages in a
log.txt file.
•Explain error-first callback in detail.

Bharat Vainsh-IT-GEC Bavnagar 77


Bibliography
E-Books:

1. Angularjs in Action ISBN 9789351198383 Ruebbelke, Wiley


Publication
2. Node.js in Action ISBN 9789386052049 Alex Young, Bradley
Meck, Mike Cantelon, Tim Oxley, Marc Harter, T.J. Holowaychuk,
Nathan Rajlich, Wiley Publication

Website :
• https://www.bharatvainsh.in/
• https://smarteportal.blogspot.com/
• https://www.w3schools.com/angular/
• https://www.javatpoint.com/nodejs-tutorial
• https://nodejs.org/en/download/
• https://www.mongodb.com/

Faculty Guide : Prof. Bharat Vainsh

Bharat Vainsh-IT-GECBhavnagar 78

You might also like