node js

28
Node Js Everything which you should know

Upload: chirag-parmar

Post on 06-May-2015

2.382 views

Category:

Documents


4 download

TRANSCRIPT

Page 1: Node js

Node Js Everything which you should know

Page 2: Node js

Node.js for Beginners

Event programming can be overwhelming for beginners, which can make Node.js difficult to get started with. But don’t let that discourage you, In this article, I will teach you some of the basics of Node.js and explain why it has become so popular.

Page 3: Node js

Introduction

To start using Node.js, you must first understand the differences between Node.js and traditional server-side scripting environments (eg: PHP, Python, Ruby, etc).

Page 4: Node js

Asynchronous Programming

Chances are good that you are familiar with asynchronous programming; it is, after all, the “A” in Ajax. Every function in Node.js is asynchronous. Therefore, everything that would normally block the thread is instead executed in the background. This is the most important thing to remember about Node.js. For example, if you are reading a file on the file system, you have to specify a callback function that is executed when the read operation has completed.

"Node.js uses a module architecture to simplify the creation of complex applications."

Page 5: Node js

You are doing Everything!

Node.js is only an environment – meaning that you have to do everything yourself. There is not a default HTTP server, or any server for that matter. This can be overwhelming for new users, but the payoff is a high performing web app. One script handles all communication with the clients. This considerably reduces the number of resources used by the application. For example, here is the code for a simple Node.js application:

Page 6: Node js

Examples

Node js var i, a, b, c, max;

max = 1000000000;

var d = Date.now();

for (i = 0; i < max; i++) {

a = 1234 + 5678 + i;

b = 1234 * 5678 + i;

c = 1234 / 2 + i;

}

console.log(Date.now() - d);

PHP$a = null; $b = null; $c = null; $i = null;

$max = 1000000000;

$start = microtime(true);

for ($i = 0; $i < $max; $i++) {

$a = 1234 + 5678 + $i;

$b = 1234 * 5678 + $i;

$c = 1234 / 2 + $i;

}

var_dump(microtime(true) - $start);

Page 7: Node js

The following table lists the response times, in milliseconds, for these two simple applications:

Number of iterations Node.js PHP

100 2.00 0.14

10’000 3.00 10.53

1’000’000 15.00 1119.24

10’000’000 143.00 10621.46

1’000’000’000 11118.00 1036272.19

Page 8: Node js

I executed the two apps from the command line so that no server would delay the apps’ execution. I ran each test ten times and averaged the results. PHP is notably faster with a smaller amount of iterations, but that advantage quickly dissolves as the number of iterations increases. When all is said and done, PHP is 93% slower than Node.js!

Node.js is fast, but you will need to learn a few things in order to use it properly.

Page 9: Node js

Modules

Node.js uses a module architecture to simply the creation of complex applications. Each module contains a set of functions related to the “subject” of the module.

Including a module is easy; simply call the require() function, like this

var variable_name('module_name');

Page 10: Node js

Global Scope

Node is a JavaScript environment running in Google’s V8 JavaScript engine.

globalVariable = 1;globalFunction = function () { ... };

Page 11: Node js

Installing New Module

Node.js has a package manager, called Node Package Manager (NPM). It is automatically installed with Node.js, and you use NPM to install new modules. To install a module, open your terminal/command line, navigate to the desired folder, and execute the following command:

npm install module_name

Page 12: Node js

Let move ahead to Javascript Framework

Page 13: Node js

Client FrameworksBackbone, Angular, Ember, etc

Server FrameworksExpress,Tower,Railway,Flatlron, etc

Real-time FrameworksDerby, meteor, SocketStream

Page 14: Node js

Thick Server - Websites

Page 15: Node js

(eg, meetup) Most functionality is still on server, but client pages are highly interactive and may use MVC for organization.

Page 16: Node js

Cutting -Edge Present

Thick-client: Web Apps, Mobile (eg, gmail) Most functionality on client. The server is just REST service marshalling data

Page 17: Node js

Little coding, large pay-off. Great for games.

Page 18: Node js

Node.js(Server)Frameworks

Page 19: Node js

Why we use a Node Framework ?

Why not vanilla Node, or Express?

•Module version compatibilities•Boilerplate code•Authentication•Database setup•Grunt/Make tasks like minification,

css/js compiling (Coffeescript, LESS, Stylus, etc)

•Socket.io•And much more

Page 20: Node js

Railway

>Full Stack Web Framework for Node.js and the Browser. Built on top of Node's Connect and Express, modeled after Ruby on Rails. Built for the client and server from the ground up.

Tower

>Railway is the Node.JS MVC framework based on ExpressJS, fully ExpressJS-compatible. It allows you to build web applications in a similar manner as in Ruby On Rails.

Express

>Express is a minimal and flexible node.js web application framework, providing a robust set of features for building single and multi-page, and hybrid web applications.

Flatlron

No one agrees on frameworks. It's difficult to get consensus on how much or how little a framework should do. Flatiron's approach is to package simple to use yet full featured components and let developers subtract or add what they want.

Page 21: Node js

DerbyMVC framework making it easy to write realtime, collaborative applications that run in both

Node.js and browsers.

Meteor is an open-source platform for building top-quality web apps in a fraction of the time, whether you're an expert developer or just getting started.

Meteor

Page 22: Node js

18 Reasons Developers Are Using It for Cloud, Mobile

Two ThirdTwo-thirds of Americans access the Web through

mobile devices, according to Engine Yard research

Thirty-Four Percent

And 34 percent of the world's top 100 Websites are using HTML5.

Page 23: Node js

The Rise of the real-time Web

Engine Yard says the growth of mobile plus the popularity of HTML5 equals the rise of the real-time Web.

And the real-time Web requires real-time technology, such as Node.js.

Event-Driven:- Node.js is a development framework that uses an event-driven, non-blocking I/O.

Built on Javascript :- Node.js is built on JavaScript to unify front-end and server-side architectures.

High Performance :- Node.js is ideal for high-performance, highly scalable, distributed real-time Web applications.

Page 24: Node js

GitHub Status :- Node.js is the second-most-watched repository on the GitHub Web-based hosting service-

second only to Bootstrap.

Ruby on Rails :- Node.js is more watched than Ruby on Rails on GitHub. Bootstrap is first, Node.js second, jQuery third and Rails fourth.

Node Package Manager :- Node Package Manager or NPM is a package manager and distribution system for Node.js. NPM has 12,000 modules and 3,800 active package authors.

Page 25: Node js

LinkedIn

LinkedIn is a prominent user of Node.js. On the server side, LinkedIn's entire

mobile software stack is completely built in Node.js. The company went from

running 15 servers with 15 instances on each physical machine to just four

instances that can handle two times the traffic.

Page 26: Node js

Walmart

Walmart re-engineered its mobile app to be powered by Node.js and pushed all its JavaScript processing to the server.

Ebay

eBay recently launched ql.io, a gateway for HTTP APIs, using Node.js as the runtime stack. eBay was able to tune a regular quality Ubuntu workstation to handle more than 120,000 active connections per Node.js process with each connection consuming about 2K of memory.

Page 27: Node js

Key Apps

Node.js is a good choice for building networked multiplayer games, interactive Websites and tools, Web analytics and online chat systems.

Page 28: Node js

Why Developer Love Node.js

Reason 1:It's JavaScript, the language of the Web.Reason 2:It supports code reuse at every level: browser,

server side and database.Reason 3:It has a strong, passionate community.Reason 4:It offers performance and scalability.Reason 5:It leads to developer happiness.