Prepared By:
Prof. Bareen Shaikh
Department of Computer
Science,
MIT ACSC, ALNDI(D), PUNE
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
Note: NPM (Node Package Manager) is included in Node.js
installation
 Node.js files contain tasks that will be
executed on certain events
 A typical event is someone trying to access a
port on the server
 Node.js files must be initiated on the server
before having any effect
 Node.js files have extension ".js"
A Node.js application consists of the following
three important components −
 Import required modules − Use
the require directive to load Node.js modules.
 Create server − A server which will listen to
client's requests similar to Apache HTTP Server.
 Read request and return response − The server
created in an earlier step will read the HTTP
request made by the client which can be a
browser or a console and return the response.
 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
Use the created http instance and
call http.createServer() method to create a
server instance
http.createServer(function (request,
response) {
response.writeHead(200, {'Content-Type':
'text/HTML});
response.end('Hello Worldn');
}).listen(8080);
console.log('Server running…”);
var http = require('http');
http.createServer(function (req, res) {
res.writeHead(200, {'Content-
Type': 'text/html'});
res.end('Hello World!');
}).listen(8080);
Note:- create a filename.js file and type above program of “Hello World”
Save file at c:usersfoldername
 Open command prompt
C:usersfoldernamenode filename.js
Server running…
 Now here your computer works as a server
 Open browser in address bar type
https://localhost:8080
 Callback is an asynchronous equivalent for a function
 A callback function is called at the completion of a
given task
 All the APIs of Node are written in such a way that
they support callbacks.
For example
A function to read a file may start reading file and
return the control to the execution environment
immediately so that the next instruction can be
executed. Once file I/O is complete, it will call the
callback function while passing the callback function,
the content of the file as a parameter. So there is no
blocking or wait for File I/O.
 This makes Node.js highly scalable, as it can process
a high number of requests without waiting for any
function to return results.
 Create a input file filename.txt
 Write some content in it
 Create .js file
 Write following code in it
var fp = require(‘fs');
var data = fp.readFileSync(‘filename.txt');
console.log(data.toString());
console.log("Program Ended");
 Run the above node in command prompt.
 Create a input file filename.txt
 Write some content in it
 Create .js file
 Write following code in it
var fp = require(‘fp');
fp.readFile(‘filename.txt', function (err, data) {
if (err) return console.error(err);
console.log(data.toString()); });
console.log("Program Ended");
 Run the above node in command prompt.
 Program ended
Blocking Non Blocking
 program blocks reads
the file and then only
it proceeds to end the
program.
 blocking program
executes very much
in sequence.
 program does not wait
for file reading and
proceeds to print
"Program Ended" and at
the same time, the
program without
blocking continues
reading the file.
 non-blocking programs
do not execute in
sequence
Introduction to Node JS1.pdf

Introduction to Node JS1.pdf

  • 1.
    Prepared By: Prof. BareenShaikh Department of Computer Science, MIT ACSC, ALNDI(D), PUNE
  • 2.
    The following tools/SDKare required for developing a Node.js application on any platform.  Node.js  Node Package Manager (NPM)  IDE (Integrated Development Environment) or TextEditor Note: NPM (Node Package Manager) is included in Node.js installation
  • 13.
     Node.js filescontain tasks that will be executed on certain events  A typical event is someone trying to access a port on the server  Node.js files must be initiated on the server before having any effect  Node.js files have extension ".js"
  • 14.
    A Node.js applicationconsists of the following three important components −  Import required modules − Use the require directive to load Node.js modules.  Create server − A server which will listen to client's requests similar to Apache HTTP Server.  Read request and return response − The server created in an earlier step will read the HTTP request made by the client which can be a browser or a console and return the response.
  • 15.
     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");
  • 16.
     Step 2- Create Server Use the created http instance and call http.createServer() method to create a server instance http.createServer(function (request, response) { response.writeHead(200, {'Content-Type': 'text/HTML}); response.end('Hello Worldn'); }).listen(8080); console.log('Server running…”);
  • 17.
    var http =require('http'); http.createServer(function (req, res) { res.writeHead(200, {'Content- Type': 'text/html'}); res.end('Hello World!'); }).listen(8080); Note:- create a filename.js file and type above program of “Hello World” Save file at c:usersfoldername
  • 18.
     Open commandprompt C:usersfoldernamenode filename.js Server running…  Now here your computer works as a server  Open browser in address bar type https://localhost:8080
  • 19.
     Callback isan asynchronous equivalent for a function  A callback function is called at the completion of a given task  All the APIs of Node are written in such a way that they support callbacks. For example A function to read a file may start reading file and return the control to the execution environment immediately so that the next instruction can be executed. Once file I/O is complete, it will call the callback function while passing the callback function, the content of the file as a parameter. So there is no blocking or wait for File I/O.  This makes Node.js highly scalable, as it can process a high number of requests without waiting for any function to return results.
  • 20.
     Create ainput file filename.txt  Write some content in it  Create .js file  Write following code in it var fp = require(‘fs'); var data = fp.readFileSync(‘filename.txt'); console.log(data.toString()); console.log("Program Ended");  Run the above node in command prompt.
  • 21.
     Create ainput file filename.txt  Write some content in it  Create .js file  Write following code in it var fp = require(‘fp'); fp.readFile(‘filename.txt', function (err, data) { if (err) return console.error(err); console.log(data.toString()); }); console.log("Program Ended");  Run the above node in command prompt.  Program ended
  • 22.
    Blocking Non Blocking program blocks reads the file and then only it proceeds to end the program.  blocking program executes very much in sequence.  program does not wait for file reading and proceeds to print "Program Ended" and at the same time, the program without blocking continues reading the file.  non-blocking programs do not execute in sequence