introduction to node.js

14
Introduction to Node.JS www.collaborationtech.co.in Bengaluru INDIA Presentation By Ramananda M.S Rao

Upload: raveendra-r

Post on 14-Feb-2017

131 views

Category:

Technology


4 download

TRANSCRIPT

Page 1: Introduction to Node.JS

Introduction to Node.JS

www.collaborationtech.co.inBengaluru INDIA

Presentation By Ramananda M.S Rao

Page 2: Introduction to Node.JS

ContentContentOverviewWhy NodeJSEnvironment SetupNode.js Console or virtual command line environment REPL Node Package ManagerGlobal vs. local package installationThe package.json configuration file or Node ConfigurationUnderstanding CPS (Continuation Passing Style)Automating tasks with GulpCommand Line InterfaceNode Process ModelSynchronous functionCall back FunctionEvent LoopNode Event LoopCreating a projectUtilitiesBuffersEvent EmittersTimersTimers and Scheduling

www.collaborationtech.co.in

Page 3: Introduction to Node.JS

Overview and Features 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.

Node.js is an open source command line tool built for the server side JavaScript code.

The JavaScript is executed by the V8, a JavaScript engine developed by Google which is used in Chrome browser. It uses a JavaScript API to access the network and file system.

With Node.js it is as simple as doing it in client-side JavaScript code. It can be easily used to create concurrent server applications. The package manager for Node.js is npm and it works very well. In many

ways it resembles package managers from other ecosystems, but npm is fast, robust, and consistent. It does a great job specifying and installing project dependencies. It keeps packages isolated from other projects, avoiding version conflicts. But it also handles global installs of shell commands and platform-dependent binaries.

www.collaborationtech.co.in

Page 4: Introduction to Node.JS

Node JS Environment SetupNode JS Base Environment Setup on WindowsThe following tools/SDK are required for developing a Node.js application. Node.js Node Package Manager (NPM) IDE (Integrated Development Environment) or TextEditor NPM Install Node.js on Windows: Visit Node.js official web site https://nodejs.org. It will automatically detect OS and display download. Download node MSI for windows by clicking on Download link. After downloading the MSI, double-click on it to start the installation. 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.

www.collaborationtech.co.in

Page 5: Introduction to Node.JS

Node.js Console - REPLNode.js Console: Node.js comes with virtual environment called REPL (aka Node shell). REPL

stands for Read-Eval-Print-Loop. It is a quick and easy way to test simple Node.js/JavaScript code.

To launch the REPL (Node shell), open command prompt (in Windows) or terminal (in Mac or UNIX/Linux) and type node as shown below. It will change the prompt to > in Windows and MAC.

You can now test pretty much any Node.js/JavaScript expression in REPL. For example, if your write "10 + 20" then it will display result 30 immediately in new line.

You can also define variables and perform some operation on them. If you need to write multi line JavaScript expression or function then just press

Enter whenever you want to write something in the next line as a continuation of your code. The REPL terminal will display three dots (...), it means you can continue on next line. Write .break to get out of continuity mode.

www.collaborationtech.co.in

Page 6: Introduction to Node.JS

Node.js Console - REPLNhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh

You can execute an external JavaScript file by writing node fileName command. For example, assume that node-example.js is on C drive of your PC with following code.

node-example.js console.log("Hello World"); Now, you can execute node-exampel.js from command prompt as shown below.

www.collaborationtech.co.in

Page 7: Introduction to Node.JS

Node.js Console - REPLnode-exampel.js from command prompt as shown below.

Run External JavaScript file To exit from the REPL terminal, press Ctrl + C twice or write .exit and press Enter.

www.collaborationtech.co.in

Page 8: Introduction to Node.JS

Node.js Console - REPLThe following table lists important REPL commands.

REPL Command Description.help Display help on all the commandstab Keys Display the list of all commands.Up/Down Keys See previous commands applied in REPL..save filename Save current Node REPL session to a file..load filename Load the specified file in the current Node REPL session.ctrl + c Terminate the current command.ctrl + c (twice) Exit from the REPL.ctrl + d Exit from the REPL..break Exit from multiline expression..clear Exit from multiline expression. Blue: "+ colors[1]);

Quit from REPL Thus, you can execute any Node.js/JavaScript code in the node shell (REPL). This will give you a result which is similar to the one you will get in the console of Google Chrome browser.

www.collaborationtech.co.in

Page 9: Introduction to Node.JS

Node.js First ApplicationCreating Node.js ApplicationStep 1: import required http moduleStep 2: create an server using http Create Server method.Step3: Pass a port 8081 to listen method.Step4: Create a js file named TestServer.jsStep5: Now run TestServer.js to see the resultStep 6: Verify the Output, Server has startedStep 7: Make a request to Node.js serverStep 8: Open http://127.0.0.1:8081/ on browser to see result.

www.collaborationtech.co.in

Page 10: Introduction to Node.JS

Node.js First Application// TestServer.jsmy_http = require("http");my_http.createServer(function(request,response){console.log("I got kicked");response.writeHeader(200, {"Content-Type": "text/plain"});response.end("Hello World");}).listen(8081);console.log("Server Running on 8081");

www.collaborationtech.co.in

Page 11: Introduction to Node.JS

Node.JS- NPMNPM (Node Package Manager) is included in Node.js installation since Node version 0.6.0., so there is no need to install it separately

www.collaborationtech.co.in

Page 12: Introduction to Node.JS

Node.js Process Model

Node.js Process Model:In this section, we will learn about the Node.js process model and understand why we should use Node.js.Traditional Web Server Model: In the traditional web server model, each request is

handled by a dedicated thread from the thread pool. If no thread is available in the thread pool at any point of time then the request waits till the next available thread.

Dedicated thread executes a particular request and does not return to thread pool until it completes the execution and returns a response.traditional web server model

www.collaborationtech.co.in

Page 13: Introduction to Node.JS

Follow us on SocialFacebook: https://www.facebook.com/collaborationtechnologies/Twitter : https://twitter.com/collaboration09Google Plus : https://plus.google.com/100704494006819853579LinkedIn : https://www.linkedin.com/in/ramananda-rao-a2012545Instagram : https://instagram.com/collaborationtechnologiesYouTube : https://www.youtube.com/channel/UCm9nK56LRbWSqcYWbzs8CUgSkype : msrnandaWhatsApp : +91 9886272445

www.collaborationtech.co.in

THANK YOU

Page 14: Introduction to Node.JS

About Us