2. What we Learn?
points
Monica Deshmane(H.V.Desai College, Pune)
Note-before this refer node JS basics.ppt
Installation of Node JS
1st program of Node JS
Node JS event loop
3. Installation of Node JS
1. Download Node.js
The official Node.js website has installation instructions
for Node.js: https://nodejs.org
2. download visual studio code for node js
3. U can show output on console-
Prog.1
.editor // Entering editor mode (^D to finish, ^C to cancel)
function welcome(name)
{ return `Hello ${name}!`; }
welcome('Node.js User');
// ^D
'Hello Node.js User!'
Setup
Monica Deshmane(H.V.Desai College, Pune)
4. Installation of Node JS
Note:- Once you have downloaded and installed Node.js on
your computer, let's try to display "Hello World" using web browser.
Create a Node.js file named "myfirst.js", and add the following code:
Prog2.
const http = require('http');
const server = http.createServer((req, res) =>
{ res.statusCode = 200;
res.setHeader('Content-Type', 'text/plain');
res.end('Hello World');
});
server.listen(3000);
});
Setup
Monica Deshmane(H.V.Desai College, Pune)
5. Creating Node.js Application
Node.js application
/* Hello, World! program in node.js */
console.log("Hello, World!")
A Node.js application consists of the following three important
components −
Node.js
application
Monica Deshmane(H.V.Desai College, Pune)
6. •Step 1 - Import Required Module
We use the require directive to load the http module
and store the returned HTTP instance into an http
variable
as follows −var http = require("http");
•Step 2 - Create Server
1. We use the created http instance and call http.createServer()
method to create a server instance.
2. then we bind it at port 8081 using the listen method
associated with the server instance.
3. Pass it a function with parameters request and response.
•Write the sample implementation to always return
• "Hello World".
Step1
7. Creating Node.js Application
•http.createServer(function (request, response)
• {
// Send the HTTP header
// HTTP Status: 200 : OK
// Content Type: text/plain
response.writeHead(200, {'Content-Type': 'text/plain'});
// Send the response body as "Hello World"
response.end('Hello Worldn');
}).listen(8081);
// Console will print the message
console.log('Server running at http://127.0.0.1:8081/');
Step2
8. Creating Node.js Application
•Step 3 - Testing Request & Response
•Now execute the main.js to start the server as follows −
$ node main.js
Verify the Output. Server has started.
Server running at http://localhost:8081/
Step3
Monica Deshmane(H.V.Desai College, Pune
9. Node JS event loop
•Node.js uses events heavily and it is also one of the
reasons why Node.js is fast compared to other similar
technologies.
•In an event-driven application, there is generally a main
loop that listens for events, and then triggers a callback
function when one of those events is detected
main loop
that listens
for events
10. Node JS event loop
•Only 1 thread is not running in thread process to achieve
parallalism.
•Node executes instructions one by one until no further
instructions to execute.
•See this loop never terminate.
var stop=false.
setTimeout(function()
{
Stop=true;
},1000);
Monica Deshmane(H.V.Desai College, Pune)
continue...
11. Node JS event loop continue…
..
•In node and browser Lot of code depends on events
Window.addEventListener(‘load’,function()
{
Alert(“window loaded”);
});
DOM API deals with events by
addEventListener(),removeEventListener() & dispatchEvent().
•It uses XMLHttpRequest object in ajax.
EventListener
Monica Deshmane(H.V.Desai College, Pune)
12. Node JS event loop
..
• this is case of example post request
when user submit form you normally listen on data
and end event request.
•http.Server(function (req,res)
{
var buf=” ”;
req.on(“data”,function(data)
{
buf+=data;
});
req.on(“end”,function(){
console.log(“all data is ready”);
});
});
Monica Deshmane(H.V.Desai College, Pune)
continue…
13. Node JS event loop
..
•U can also do like
•const http = require('http');
•const server =http.createServer((req, res) =>{
•var buf='hiiiii';
•req.on('end',console.log('all data is ready‘ + buf));
•}).listen(8081);
•console.log('Server running at http://localhost:8081/');
Monica Deshmane(H.V.Desai College, Pune)
continue…
14. What we Learned?
points
Monica Deshmane(H.V.Desai College, Pune)
2.1 Introduction
2.2 What is Node ?
2.3 Traditional Web Server Model
2.4 Node JS Process model
2.5 Installation of Node JS
2.6 Node JS event loop
15. Questions..
1. What is node?why it is used?
2. What can node js do?
3. Expain traditional web server model with diagram.
4. Expain node js server model with diagram.
5. What is difference between node server & tradional server model
?explain any 4 points of differenciation.
6. Write program to display heloo msg on server using node js.
7. What are features of node js?explain each in brief.
8. What are advantages of node js?
9. What are dis-advantages of node js?
16. 1mark questions-
What is libuv?
What is callback?
What is event loop?
Which is The official Node.js website?
What is require?
How to execute js in node js?