The Server-side JavaScript
AGENDA
 Introduction
 NodeJS
– Asynchronous nature
 MongoDB
– Data Model
– JSON based storage
– Querying
BACKGROUND
 V8 is an open source JavaScript engine
developed by Google.
 Node.js runs on V8
 It was created by Ryan Dahl in 2009.
 Is Open Source. It runs well on Linux
systems, can also run on Windows systems.
Similar to EventMachine (Ruby) or Python’s
Twisted or Perl’s AnyEvent
INTRODUCTION: BASIC
 In simple words Node.js is ‘server-side
JavaScript’.
 In not-so-simple words Node.js is a high-
performance network applications
framework, well optimized for high
concurrent environments.
‘Node's goal is to provide an easy way to
build scalable network programs’ - (from
nodejs.org!)
GETTING STARTED & HELLO WORLD
 Install/build Node.js.

When you are done, open cmd/terminal and
type this:
‘node YOUR_FILE.js’
Here is a simple example, which prints ‘hello
world’
var sys = require(“sys”);
sys.puts(“hello”);
SOME THEORY: NON-BLOCKING I/O
 Traditional I/O
var result = db.query(“select x from table_Y”);
doSomethingWith(result); //wait for result!
doSomethingWithOutResult(); //execution is
blocked!
 Non-traditional, Non-blocking I/O
db.query(“select x from table_Y”,function (result)
{
doSomethingWith(result); //wait for result!
});
doSomethingWithOutResult(); //executes without any
delay!
WHAT CAN YOU DO WITH NODE.JS ?
 You can create an HTTP server and print ‘hello
world’ on the browser in just 4 lines of JavaScript.
(Example included)
 You can create a TCP server similar to HTTP
server, in just 4 lines of JavaScript. (Example
included)
 You can create a DNS server.
 You can create a Static File Server.
 You can create a Web Chat Application like
GTalk in the browser.
 Node.js can also be used for creating online
games, collaboration tools or anything which
sends updates to the user in real-time.
WHEN TO USE NODE.JS?
 Node.js is good for creating streaming based
real-time services, web chat applications,
static file servers etc.
 If you need high level concurrency and not
worried about CPU-cycles.
 If you are great at writing JavaScript code
because then you can use the same
language at both the places: server-side and
client-side.
Java IO
NODEJS
RESOURSCE TO GET STARTED
 Watch this video at Youtube:
http://www.youtube.com/watch?v=jo_B4LTHi3
I
 Read the free O’reilly Book ‘Up and Running
with Node.js’ @
http://ofps.oreilly.com/titles/9781449398583/
 Visit www.nodejs.org for Info/News about
Node.js
 Watch Node.js tutorials @
http://nodetuts.com/
 For Info on MongoDB:
http://www.mongodb.org/display/DOCS/Home
 For anything else Google!
SOME GOOD MODULES
 Express – to make things simpler e.g.
syntax, DB connections.
 Jade – HTML template system
 Socket.IO – to create real-time apps
 Nodemon – to monitor Node.js and push
change automatically
 CoffeeScript – for easier JavaScript
development
 Find out more about some widely used
Node.js modules at:
http://blog.nodejitsu.com/top-node-module-cr
eators
EXAMPLE: LETS CONNECT TO A DB
(MONGODB)
 Install mongojs using npm, a mongoDB driver for
Node.js
npm install mongojs
 Code to retrieve all the documents from a
collection:
var db = require("mongojs")
.connect("localhost:27017/test", ['test']);
db.test.find({}, function(err, posts) {
if( err || !posts) console.log("No posts found");
else posts.forEach( function(post) {
console.log(post);
});
});
JSON OVERVIEW
 Similar to XML – lightweight
–
 JavaScript Objects
– Natively parseable by JS (eval)
 AJAX
- Language for REST based API design
Nodejs presentation