Introduction to Node.
js®
Jitendra Kumar Patel
Saturday, January 31, 2015
Why [Link] ?
“Node's goal is to
provide an easy way to
build scalable Network
programs”
About [Link]……
• Created by Ryan Dahl in 2009
• Development && maintenance sponsored by Joyent
• Licence MIT
• Last release : 0.10.31
• Based on Google V8 Engine
• +99 000 packages
Success Stories…..
Another project like [Link]….
● Vert.x => Polygot programming
● Akka => Scala and Java
● Tornado => Python
● Libevent => C
● EventMachine => Ruby
[Link] not……
● Another Web framework
● For beginner
● Multi-thread
Development for the Web….
• Traditional desktop applications have a user interface wired up with background logic
– user interfaces are based on Windows Forms, Jswing, WPF, Gtk, Qt, etc. and dependant on operating
system
• On the web user interfaces are standardized
– HTML for markup
– CSS for style
– JavaScript for dynamic content
• In the past proprietary technologies existed on the web, e.g. Adobe Flash, ActiveX
– They are slowly dying
• Data is generated on the server, transferred to the client and displayed by the browser
• Server Side technologies include PHP, JSP, [Link], Rails, Djanog, and yes, also [Link]
REST……
● REST = REpresentational State Transfer
● Not new technology
● Architectural style for client-server
Goals of REST……..
● General interface
● Scalability of component interface
● Reduce latency
● Encapsulate legacy system
HTTP Method……
● GET
● POST
● PUT
● DELETE
HTTP Method with CRUD…..
● POST => Create
● GET => Read
● PUT => Update
● DELETE => Delete
Why [Link] ?
● Non Blocking I/O
● V8 Javascript Engine
● Single Thread with Event Loop
● 40,025 modules
● Windows, Linux, Mac
● 1 Language for Frontend and Backend
● Active community
What is [Link]?
• Asynchronous i/o framework « A platform built on
Chrome's JavaScript
runtime for easily
• Core in c++ on top of v8
building fast, scalable
• Rest of it in javascript network applications. »
• Swiss army knife for network Related
[Link]
stuffs
• Can handle thousands of Concurrent connections with
Minimal overhead (cpu/memory) on a single process
• It’s NOT a web framework, and it’s also NOT a language
Why javascript ?!!!
• Friendly callbacks
• Ubiquitous
• No I/o Primitives
• One language to RULE them all
JavaScript is well known for client-side scripts running inside the
browser
[Link] is JavaScript running on the server-side
SSJS -> Server-Side JavaScript
Use a language you know
Use the same language for client side and server side
JavaScript Engines……
The idea behind [Link]….
Perform asynchronous processing on single thread instead of classical multithread processing,
minimize overhead & latency, maximize scalability
Scale horizontally instead of vertically
Ideal for applications that serve a lot of requests but dont use/need lots of computational power
per request
Not so ideal for heavy calculations, e.g. massive parallel computing
Also: Less problems with concurrency
[Link] Event Loop
There are a couple of implications of this apparently very simple and basic model
• Avoid synchronous code at all costs because it blocks the event loop
• Which means: callbacks, callbacks, and more callbacks
Blocking vs Non-Blocking……
Example :: Read data from file and show
data
Blocking…..
● Read data from file
● Show data
● Do other tasks
var data = [Link]( “[Link]” );
[Link]( data );
[Link]( “Do other tasks” );
Non-Blocking…… Callback
● Read data from file
When read data completed, show data
● Do other tasks
[Link]( “[Link]”, function( err, data ) {
[Link](data);
});
When to use it ?
• Chat/Messaging
• Real-time Applications
• Intelligent Proxies
• High Concurrency Applications
• Communication Hubs
• Coordinators
[Link] for….
● Web application
● Websocket server
● Ad server
● Proxy server
● Streaming server
● Fast file upload client
● Any Real-time data apps
● Anything with high I/O
Getting Started…..
• [Link] and Download [Link]
• Extract to any directory
• $ ./configure && make install
File [Link]…..
Project informations
• Name
• Version
• Dependencies
• Licence
• Main file
Etc...
[Link] Modules…..
● [Link]
● # of modules = 1,21,943
Install module…..
$npm install <module name>
Using module…..
• var http = require(‘http’);
• var fs = require(‘fs’);
• var express = require(‘express’);
In NodeJS
Others modules…..
Favorite Modules…..
● express
● underscore
● request
● async
● mysql
Find more in [Link]…..
Tools of the trade…..
• IDEs for [Link]
– online IDE like cloud9
– Eclipse with [Link] plugins
– Visual Studio [Link] Toolkit
– Jetbrains Webstorm
– lots of other IDEs: webmatrix, coderunner, Expresso, Atom, etc.
• Preferred : Jetbrains Webstorm & Visual Studio with [Link]
Express…
[Link]
Express As Middleware
Middleware can:
• Execute any code.
• Make changes to the request and the response objects.
• End the request-response cycle.
• Call the next middleware in the stack.
Express Middleware
Modules….
Working with Express…..
● Inspire from Sinatra
● Fast
● Flexible
● Simple
Some Warnings…
• Release stable 0.10.36 (young)
• Lots of stuffs to look at
• Lots of half backed stuffs
• Retro compatibility???
• Bad at handling staticcontents
• Bad at handling binarycontents
• Hard to find organized and authoritative informations
Jitendra Kumar Patel
[Link]
[Link]@[Link]
@bewithjitendra
[Link]/bewithjitendrapatel
Saturday, January 31, 2015