Introduction to node.js

Post on 10-May-2015

3.406 views 0 download

Tags:

Transcript of Introduction to node.js

ByArun Kumar Arjunan,Product Architect,Chronus

Introduction to Node.js

AGENDA

• About Node.js

• Programming in Node.js

• Web Frameworks

• Node.js internals

• Popularity of Node.js

• Downside of Node.js

ABOUT NODE.JS

What is

Node.js?

ABOUT NODE.JS

Node.js

Applications

node.js KO

NIDE

Chess@home

GITJS

GitJS is a pure JavaScript implementation

Of Git.

Example Commands:• git.js log• git.js branch• git.js show SHA

NODECHAT

DEFINITION

Node.js is an

event-driven I/O

server-side

JavaScript environment

AGENDA

• About Node.js

Programming in Node.js

• Web Frameworks

• Node.js internals

• Popularity of Node.js

• Downside of Node.js

HELLO WORLD PROGRAM

Node.JS is a command line tool

$ node hello-console.js

HTTP SERVER

FILE SYSTEM

OTHER LIBRARIES

• Timers

• Events

• Buffers

• Streams

• Crypto

• TLS/SSL

• DNS

• REPL

• Child Processes

• ZLIB

• OS

NODE IS EVENT DRIVEN

Did you notice that the previous examples use Event Driven

technique?

NODE IS EVENT DRIVEN

NODE IS EVENT DRIVEN

NODE IS EVENT DRIVEN

$ ab –n 200 -c 50 http://localhost:8080/

NOW IN RUBY

$ ab –n 200 -c 50 http://localhost:1337/

require 'rubygems' require 'mongrel'

class HelloWorldHandler < Mongrel::HttpHandler def process(request, response) sleep a2 response.start(200) do |head,out| head["Content-Type"] = "text/plain" out.write(“Hello World”) end end end

h = Mongrel::HttpServer.new("127.0.0.1", "1337") h.register("/", HelloWorldHandler.new) h.run.join

EVENT DRIVEN VS THREADING

Lets compare Event Driven Programming & Thread based programming!!!

• Some theory proves Threading model is better

• Some proves Event Driven Programming is better

EVENT DRIVEN VS THREADING

Lets take an example that is

relevant to us!!!

APACHE VS NGINX

APACHE VS NGINX

APACHE VS NGINX

The difference?

Apache uses one thread perconnection.

NGINX doesn’t use threads. It usesan event loop

Disadvantages of Threading

• Context switching is not free• Execution stacks take up

memory• For massive concurrency,

cannot use an OS thread for each connection.

AGENDA

• About Node.js

• Programming in Node.js

Web Frameworks

• Node.js internals

• Popularity of Node.js

• Downside of Node.js

NODE.JS COMMUNITY

Node.js has excellent

community

NODE PACKAGE MANAGER

NPMIts like bundler for Ruby Gems

AROUND 60 TEMPLATE ENGINES

asyncEJS, bake, bind-js, blue, CoffeeKup, combyne.js, doT.js, dust, Eco, ejs, haml.js, haml-js, jshtml, jqtpl, Jade, jazz, JinJS, JSON Template, jm, jsdom, less.js, Liquor, minimal.js, Mu, nTPL, nodejs-meta-templates, normal-template, nun, node.magic_dom, node-template, node-properties, Parrot, PURE, stencil, Node-T, Swig, Templ8, template.node.js, tmpl-node, TwigJS, weld

WEB FRAMWEORKS

1. Geddy2. ExpressJS3. RailwayJS

RAILWAYJS

Quick guide to RailwayJS

Its like Rails itself

ROUTES IN RAILWAYJS

CONTROLLERS IN RAILWAYJS

MODELS IN RAILWAYJS

AGENDA

• About Node.js

• Programming in Node.js

• Web Frameworks

• Node.js Internals

• Popularity of Node.js

• Downside of Node.js

NODE.JS ARCHITECTURE

ABOUT V8

• V8 team is led by Lars Bak

• Lars Bak was the technical lead behind HotSpot (Sun’s

Java VM).

• HotSpot improved Java’s performance 20x times

• Before HotSpot Lars Bak worked on a Smalltalk VM

• V8 uses hidden classes

UNDERSTANDING V8

Lets try to

understand V8

UNDERSTANDING V8

• It is used to run JavaScript

• Can be embedded into any application

• Can expose C methods & Objects using

JS interface

USING V8

USING V8

String::New(“a = 1; b = 2; a + b”)

$ ./hello-world

String::New(ARGV[1])

$./hello-world “a = 1; b = 2; a + b”

String::New(fread(ARGV[1]))

$./hello-world hello-world.js

NODE.JS ADVANTAGES

So Node.Js is nothing but a set

of libraries using V8 engine

AGENDA

• About Node.js

• Programming in Node.js

• Web Frameworks

• Node.js Internals

• Popularity of Node.js

• Downside of Node.js

NODE.JS ADVANTAGES

• Server side Javascript

• Javascript is dynamic

• Event based

• Learning curve is less

• Suited for event driven programming

• Uses V8 which relatively faster

• Uses CommonJS module spec

NODE.JS ADVANTAGES

Is the server side

JavaScript

programming new?

SERVER SIDE JS FRAMEWORKS

• Helma - Rhino

• AppJet - Rhino

• Aptana Jaxer - SpiderMonkey

• CouchDB - SpiderMonkey

NODE.JS ADVANTAGES

Is the

Event Driven

programming new?

EVENT DRIVEN PROGRAMMING LANGUAGES

• E Programming language

• Event Driven MVC Framework for SmallTalk

• AmbientTalk

Similar Frameworks

• Event Machine for Ruby

• Libevent for C

• Twisted for Python

Conclusion

So why is Node.js so

popular?

Conclusion

I think its because

Ryan Dhal put cool

things together!!!

AGENDA

• About Node.js

• Programming in Node.js

• Web Frameworks

• Node.js Internals

• Popularity of Node.js

• Downside of Node.js

NODE.JS COMPLAINTS

• Programmers are used to synchronous code

• Exceptions are hard to handle

• Difficult to debug

• Most importantly it is not matured yet

• Doesn’t utilize multi-core CPU

Conclusion

FUTURE OF

NODE.JS

???