55
By Arun Kumar Arjunan, Product Architect, Chronus Introduction to Node.js

Introduction to node.js

Embed Size (px)

Citation preview

Page 1: Introduction to node.js

ByArun Kumar Arjunan,Product Architect,Chronus

Introduction to Node.js

Page 2: 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

Page 3: Introduction to node.js

ABOUT NODE.JS

What is

Node.js?

Page 4: Introduction to node.js

ABOUT NODE.JS

Node.js

Applications

Page 5: Introduction to node.js

node.js KO

Page 6: Introduction to node.js

NIDE

Page 7: Introduction to node.js

Chess@home

Page 8: Introduction to node.js

GITJS

GitJS is a pure JavaScript implementation

Of Git.

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

Page 9: Introduction to node.js

NODECHAT

Page 10: Introduction to node.js

DEFINITION

Node.js is an

event-driven I/O

server-side

JavaScript environment

Page 11: 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

Page 12: Introduction to node.js

HELLO WORLD PROGRAM

Node.JS is a command line tool

$ node hello-console.js

Page 13: Introduction to node.js

HTTP SERVER

Page 14: Introduction to node.js

FILE SYSTEM

Page 15: Introduction to node.js

OTHER LIBRARIES

• Timers

• Events

• Buffers

• Streams

• Crypto

• TLS/SSL

• DNS

• REPL

• Child Processes

• ZLIB

• OS

Page 16: Introduction to node.js

NODE IS EVENT DRIVEN

Did you notice that the previous examples use Event Driven

technique?

Page 17: Introduction to node.js

NODE IS EVENT DRIVEN

Page 18: Introduction to node.js

NODE IS EVENT DRIVEN

Page 19: Introduction to node.js

NODE IS EVENT DRIVEN

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

Page 20: Introduction to node.js

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

Page 21: Introduction to node.js

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

Page 22: Introduction to node.js

EVENT DRIVEN VS THREADING

Lets take an example that is

relevant to us!!!

Page 23: Introduction to node.js

APACHE VS NGINX

Page 24: Introduction to node.js

APACHE VS NGINX

Page 25: Introduction to node.js

APACHE VS NGINX

The difference?

Apache uses one thread perconnection.

NGINX doesn’t use threads. It usesan event loop

Page 26: Introduction to node.js

Disadvantages of Threading

• Context switching is not free• Execution stacks take up

memory• For massive concurrency,

cannot use an OS thread for each connection.

Page 27: 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

Page 28: Introduction to node.js

NODE.JS COMMUNITY

Node.js has excellent

community

Page 29: Introduction to node.js

NODE PACKAGE MANAGER

NPMIts like bundler for Ruby Gems

Page 30: Introduction to node.js

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

Page 31: Introduction to node.js

WEB FRAMWEORKS

1. Geddy2. ExpressJS3. RailwayJS

Page 32: Introduction to node.js

RAILWAYJS

Quick guide to RailwayJS

Its like Rails itself

Page 33: Introduction to node.js

ROUTES IN RAILWAYJS

Page 34: Introduction to node.js

CONTROLLERS IN RAILWAYJS

Page 35: Introduction to node.js

MODELS IN RAILWAYJS

Page 36: 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

Page 37: Introduction to node.js

NODE.JS ARCHITECTURE

Page 38: Introduction to node.js

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

Page 39: Introduction to node.js

UNDERSTANDING V8

Lets try to

understand V8

Page 40: Introduction to node.js

UNDERSTANDING V8

• It is used to run JavaScript

• Can be embedded into any application

• Can expose C methods & Objects using

JS interface

Page 41: Introduction to node.js

USING V8

Page 42: Introduction to node.js

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

Page 43: Introduction to node.js

NODE.JS ADVANTAGES

So Node.Js is nothing but a set

of libraries using V8 engine

Page 44: 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

Page 45: Introduction to 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

Page 46: Introduction to node.js

NODE.JS ADVANTAGES

Is the server side

JavaScript

programming new?

Page 47: Introduction to node.js

SERVER SIDE JS FRAMEWORKS

• Helma - Rhino

• AppJet - Rhino

• Aptana Jaxer - SpiderMonkey

• CouchDB - SpiderMonkey

Page 48: Introduction to node.js

NODE.JS ADVANTAGES

Is the

Event Driven

programming new?

Page 49: Introduction to node.js

EVENT DRIVEN PROGRAMMING LANGUAGES

• E Programming language

• Event Driven MVC Framework for SmallTalk

• AmbientTalk

Page 50: Introduction to node.js

Similar Frameworks

• Event Machine for Ruby

• Libevent for C

• Twisted for Python

Page 51: Introduction to node.js

Conclusion

So why is Node.js so

popular?

Page 52: Introduction to node.js

Conclusion

I think its because

Ryan Dhal put cool

things together!!!

Page 53: 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

Page 54: Introduction to 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

Page 55: Introduction to node.js

Conclusion

FUTURE OF

NODE.JS

???