nodejs guide for beginners

41
BY C.ENOCH JOSHUA

Upload: enoch-joshua

Post on 06-Apr-2017

40 views

Category:

Software


1 download

TRANSCRIPT

Page 1: NodeJS guide for beginners

BYC.ENOCH JOSHUA

Page 2: NodeJS guide for beginners

What’s I/O?Distance of data travel from input to output.

Page 3: NodeJS guide for beginners

What is JavaScript ? It is dynamic programming language.

It is client side script to interact with user.

It most commonly used as a part of web browser

Page 4: NodeJS guide for beginners

Background Node.js was invented in 2009 by Ryan Dahl inspired to create Node.js after seeing a file upload progress

bar on FLICKR Open source , sponsored by Joyent and Microsoft Ryan was a mathematics student and dropped out of

college. Node.js runs on V8.

Page 5: NodeJS guide for beginners

Introduction: Basic

What is NodeJS ?NodeJS is an open source , cross platform runtime environment for server side and networking application. It is written in JavaScript and can run on Linux , Mac ,

Windows , FreeBSD. It provided an event driven architecture and a non blocking I/O

that optimize and scalability. These technology uses for real time application.

It used Google JavaScript V8 Engine to Execute Code.

Page 6: NodeJS guide for beginners

JavaScript V8 EngineThe V8 JavaScript Engine is an open source JavaScript engine developed by Google for the Google Chrome web browser.V8 compiles JavaScript to native machine code (IA-32, x86-64, ARM, or MIPS ISAs) before executing it.V8 can run standalone, or can be embedded into anyC++ application

Page 7: NodeJS guide for beginners

Why Use NodeJS? Node's goal is to provide an easy way to build scalable

network programs.

What is unique about NodeJS? Using JavaScript on server-side making communication

between client and server will happen in same language. Servers are Event based serves each request in Evented

Loop. So, it can handle simultaneous requests.

Page 8: NodeJS guide for beginners

Reasons to use NodeJS It is very lightweight and fast. There has been over 200,000

visits on this website in three weeks and minimal server resources has been able to handle it all.

The counter is really easy to make to be real time. Node.js was easy to configure. There are lots of modules available for free. NodeJS work with NoSQL as well.

Page 9: NodeJS guide for beginners

When not use NodeJS Your server request is dependent on heavy CPU consuming

algorithm/Job.

NodeJS SetupDownload at http://nodejs.orgSet Environment variable (Last version automatic set)

Open command line and type following.

node -v

Page 10: NodeJS guide for beginners

What is Callback? Call-back is an asynchronous equivalent for a function. A call-back function is called at the completion of a given

task. Node makes heavy use of call-backs.

Page 11: NodeJS guide for beginners

Blocking I/O: Traditional applications waits for a set code to complete before

it displays the content. This is called blocking code.

Page 12: NodeJS guide for beginners

Non blocking I/O: In non blocking Once the request is made we continue on to

the next line of code before waiting for the time consuming request to finish.

Uses callback functions to handle non blocking code.

Page 13: NodeJS guide for beginners

What makes node better?

Event driven

• Just like java script events on the client, Node can handle various events on the server

• Examples of server events are file transfer , request for a page , loading content from database

Page 14: NodeJS guide for beginners

Node.js Event Loop

Page 15: NodeJS guide for beginners

REPL TerminalRead − 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 − Loops the above command until the user presses ctrl-

c twice.

Starting REPL

Simple Expression

node >

node > 1 + 3 4>1 + ( 2 * 3 ) - 4 3>

Page 16: NodeJS guide for beginners

REPL Commandsctrl + c − terminate the current command.ctrl + c twice − terminate the Node REPL.ctrl + d − terminate the Node REPL.Up/Down Keys − see command history and modify previous

commands.tab Keys − list of current commands..help − list of all commands..break − exit from multiline expression..clear − exit from multiline expression..save filename − save the current Node REPL session to a file..load filename − load file content in current Node REPL

session.

Page 17: NodeJS guide for beginners

What is npm?

NPM

Easy for developers to share and reuse code

Makes it easy to update the code that you’re sharing

Page 18: NodeJS guide for beginners

NPM Functionalities

Functionalities

Online repositories

Command line utility

To install Version management

Dependency management

Page 19: NodeJS guide for beginners

The npm comes bundled with Node.js installables in versions after that v0.6.3. You can check the version by command.

npm –v (or) npm version

Page 20: NodeJS guide for beginners

Installing Modules using npm

Following is the syntax to install any Node.js module:

For example:npm install <Module Name>  

npm install express    

Page 21: NodeJS guide for beginners

Local Installation

Global Installation

• Local Mode

Package installation

• Node module

accessible• require(

)

command

• Npm ls

• Global Mode

Package installation

• System Directory

Not-accessible • Require(

)

command

• Npm ls -g

CLI

Page 22: NodeJS guide for beginners

Uninstalling a Modulenpm uninstall express

Updating a Modulenpm update express

Search a Modulenpm search express

Page 23: NodeJS guide for beginners

BUFFER For an input operation, buffer mode will wait until all the data

is collected into a buffer and then pass it to a call-back as soon as the entire resource

is read. In the below image you can see that when some data are

received it is saved into the buffer. But when everything is received i.e. the final one which

causes the entire buffer to be sent to the customer.

Page 24: NodeJS guide for beginners

STREAMS Whereas stream will allow you to process the data as soon as

it arrives from the resource. In the below image each new chunk of data is received from

the resource is immediately provided to the consumer, thus consumer can process the data straightaway without

waiting for all the data to be collected in the buffer.

Page 25: NodeJS guide for beginners

Stream Types

Types of Streams

used for read operation.

Readable

used for write operation.

Writeable

used for both read and write

operation.

Duplex

output is computed based on

input.Transform

Page 26: NodeJS guide for beginners

File System Node implements File I/O using simple wrappers around

standard POSIX functions. The Node File System (fs) module can be imported using the

following syntax File system is of 2 types (i) Synchronous (ii) Asynchronous

Synchronous: synchronous method never blocks a program during its

execution, whereas the second one does.

var fs = require("fs")

Page 27: NodeJS guide for beginners

Asynchronous Asynchronous methods take the last parameter as the

completion function call-back and the first parameter of the call-back function as error.

It is better to use an asynchronous method instead of a synchronous method

Page 28: NodeJS guide for beginners

Scalable

Node applications are highly scalable

Horizontal scaling of servers

Adding more servers instead of increasing the performance of each

Page 29: NodeJS guide for beginners

What is a Web Server?

A Web Server is a software application which handles HTTP requests

sent by the HTTP client, like web browsers, and returns web pages in response to the clients.

Web servers usually deliver html documents along with images, style sheets, and scripts.

Most of the web servers support server-side scripts

Page 30: NodeJS guide for beginners

Web Application Architecture

Client − This layer consists of web browsers, mobile browsers or applications which can make HTTP requests to the web server.

Server − This layer has the Web server which can intercept the requests made by the clients and pass them the response.

Business − This layer contains the application server which is utilized by the web server to do the required processing. This layer interacts with the data layer via the database or some external programs.

Data − This layer contains the databases or any other source of data.

Page 31: NodeJS guide for beginners
Page 32: NodeJS guide for beginners

Who uses NodeJS

Page 33: NodeJS guide for beginners

WHAT IS

Page 34: NodeJS guide for beginners

Full-stack JavaScript solution that helps you build fast, robust and maintainable production web applications using:

WHAT DOES IT M.E.A.N.?

express

Page 35: NodeJS guide for beginners
Page 36: NodeJS guide for beginners

WHY M.E.A.N.?SAME LANGUAGE, SAME OBJECTS.

{“_id”: ObjectId(“512638a28b799”), “username” : “symonny” }

{“_id”: “512638a28b799”,“username” : “symonny” }

{“_id”: “512638a28b799”,“username” : “symonny” }

Page 37: NodeJS guide for beginners

❖ NoSqL cross-platform document-orienteddatabase system

❖ JSON-like documents with dynamic schemas

❖ Easier & faster to send data between

❖ client and server (b/c data saved in JSON format)

❖ Local Install:

➡ MongoDB: http://www.mongodb.org/

❖ MongoDB-as-a-Service:

➡ Modulus: https://modulus.io

➡ Mongolab: https://mongolab.com

Page 38: NodeJS guide for beginners

❖ A lightweight framework used to build single and multi-page web applications in Node.JS

❖ Wrapper for the core Node.js HTTP module objects.

❖ Provides functions for everythingyou may need to build a modern web server

❖ Learn More: http://expressjs.com

express

Page 39: NodeJS guide for beginners

❖ Client-side MVC framework: http://angularjs.org❖ Problem: Updating page without reload

Angular.js declarative, 2-way data❖ Solution: binding

Page 40: NodeJS guide for beginners

JavaScript platform built on V8❖ Server-side engine

❖ Helps building highly scalable and concurrent applications rapidly

❖ Makes multithreaded server easy

❖ Event based concurrency

❖ Easy to modify and maintains apps, due to piped modules

❖ Install from http://nodejs.org

Page 41: NodeJS guide for beginners

APPS SUITED FOR NODE.JS

❖ E-Commerce

❖ Payment Processing

❖ Social Media

❖ Realtime Services

❖ Media Applications

❖ Enterprise Services