Se ha denunciado esta presentación.
Se está descargando tu SlideShare. ×

Introduction to Node JS1.pdf

Anuncio
Anuncio
Anuncio
Anuncio
Anuncio
Anuncio
Anuncio
Anuncio
Anuncio
Anuncio
Anuncio
Anuncio
Cargando en…3
×

Eche un vistazo a continuación

1 de 23 Anuncio

Más Contenido Relacionado

Similares a Introduction to Node JS1.pdf (20)

Anuncio

Más reciente (20)

Introduction to Node JS1.pdf

  1. 1. Prepared By: Prof. Bareen Shaikh Department of Computer Science, MIT ACSC, ALNDI(D), PUNE
  2. 2. 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
  3. 3.  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"
  4. 4. 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.
  5. 5.  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");
  6. 6.  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…”);
  7. 7. 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
  8. 8.  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
  9. 9.  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.
  10. 10.  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.
  11. 11.  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
  12. 12. 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

×