2. What is NodeJS ?
NodeJS is a platform built on Google’s V8
javascript engine for easily building scalable
network applications.
Advantages
Event driven
Nonblocking I/O Model
Perfect for data intensive real time applications
Lightweight and efficient
3. More about NodeJS
Is it all about developing server side code with
Javascript ?
Javascript provides an interface to V8.
What is this buzz about Google’s V8 javascript engine ?
Powers Google Chrome.
Translates javascript to assembly code
Crankshaft JIT compiler
How is NodeJS different from other scripting
paradigms like Perl/PHP/Python/Ruby on Rails?
Non Blocking
Single threaded
In built event based approach
7. How Non-Blocking I/O helps ?
E
var mysql = require('db-mysql'); r
new mysql.Database( o
{ r
hostname: 'localhost‘,user: 'root‘,
password: '‘, database: 'kino_badge’}).on('error', function(error) {
console.log('ERROR: ' + error);
}).on('ready', function(server) { On Ready
console.log('Connected to ' + server.hostname + ' (' + server.version + ')');
}).connect(function(error){
if(error){
connected
console.log("connection error " + error);
}
this.query().select('*').from('appUser').execute(function(error,rows,columns){
if(error){
console.log("Error: " + error); R
return;
} e
console.log("Rows : " + JSON.stringify(rows)); s
}); u
});
l
t
8. Why Javascript is the chosen one ?
Inherently single threaded.
Functions and Closures are available as first
class objects
No preconceived notions about I/O
Event orientation at its core
Present everywhere
Browsers
Webkits
9. NodeJS community and NPM
What is NPM
Node packaged module.
Third party Libraries, can be used with nodejs
What all modules are available for application
development
16762 open source libraries available, and counting.
To install dependencies using NPM
Any package can be installed using npm install
Full instruction set can be referred at
https://npmjs.org/doc/install.html
10.
11. NodeJS in real world !!
• Linked-In Mobile app.
• Four Square Application
12. Want to program in ?
Be event oriented
Think in terms of callbacks on event
completion and error occurrences
Design using closures and callbacks
13. Let’s code with
A hello world program
A http server
List files on server
14. How callback works ?
console.log(‘Hello’);
setTimeout(function(){ callback
console.log(‘World’);
},2000);
15. Callbacks revisited..
var http = require("http"); HTTP Module
Create Server
http.createServer(
function(request,response){ Callback on request
response.writeHead(200,{'Content-Type'
: 'text/plain'});
response.end("Hi I am running on
NodeJSn");
}
).listen(8124,'127.0.0.1'); Listen to Port
console.log("server running at
http://localhost:8124");
16. Detailed Example
var fs = require('fs');
var express = require('express');
var app = express(); app.listen(3000);
fs.readdir('../',function(err,files){
console.log(files.length);
});
app.get('/file/index/:user', function(request, response) {
var user_directory = request.params.user;
fs.readdir(user_directory,function(err,files){
if(err){
console.log(err);
response.send(JSON.stringify(err));
}else{
response.set('Content-Type', 'text/html');
var dyna_html =
'<html><head></head><body><table style="width:100px; height;200px;border: 1px solid black;">';
for(var i=0;i<files.length;i++){
var dyna_col = '<tr><td>'+files[i]+'</td></tr>'
dyna_html += dyna_col;
}
dyna_html +='</table></body></html>';
response.send(200, dyna_html); }});});
17. Let’s break with NodeJS
How to break with NodeJS
Programming modularly
Developing larger applications
Developing for the Web
ExpressJS
MVC REST
19. Complete JavaScript EcoSystem
Developing REST based Web app with
ExpressJS
Adding persistence layer with MongoDB
Managing security with OAuth using
Google - PassportJS
Facebook - facebook-node-sdk
Communicating with the Client side javascript
implemented in SmartClientJS