SlideShare a Scribd company logo
Node.js :: Introduction — Part 2
PLAN
Node.js
Frameworks	&	Libs
Demo	application
NODE.JS
																																														is	bad	for:
CPU	heavy	tasks
Doing	everything	with	Node
NODE.JS
																																																											is	good	for:
Prototyping
REST	/	JSON	APIs
SPA
Streaming	data
Real-time	apps
MODULES
Node	Package	Manager

~55k	modules	available
NPM
	npm	install	express
	npm	install	express	-g
	npm	install	express	--save
	npm	install	nodemon	--save-dev
PACKAGE.JSON
	{
		"name":	"application-name",
		"version":	"0.0.1",
		"private":	true,
		"scripts":	{
				"start":	"node	app.js"
		},
		"dependencies":	{
				"express":	"3.4.8",
				"jade":	"*"
		},
		"devDependencies:	{
		}
	}
	npm	publish	<tarball>
	npm	publish	<folder>
HTTP	SERVER
	var	http	=	require('http');
	http.createServer(function	(req,	res)	{
			res.writeHead(200,	{'Content-Type':	'text/plain'});
			res.end('Hello	World!');
	}).listen(3000);
	node	server.js
WHAT'S	INSIDE
HTTP,	HTTPS	&	TCP	interfaces
File	System	I/O
Streams
Child	Process
Cluster
.	.	.
NETWORK	INTERFACES
HTTP/HTTPS/TCP
	//	require	a	module
	var	server	=	http	||	https	||	net;
	server.createServer([requestListener]).listen(port,	[callback]);
FILE	SYSTEM	I/O
	var	fs	=	require('fs');
	fs.readFile(filename,	[options],	callback);
	fs.readFileSync(filename,	[options]);
	fs.writeFile(filename,	data,	[options],	callback);
	fs.writeFileSync(filename,	data,	[options]);
	fs.rmdir(path,	callback);
	fs.unlink(path,	callback);
	fs.readdir(path,	callback);
STREAMS
Readable	(req,	fs,	stdout,	stderr)
Writable	(res,	fs,	stdin)
Duplex	(TCP	sockets,	crypto)
Transform	(zlib,	crypto)
STREAMS	USE	CASE
	http.createServer(function	(req,	res)	{
			fs.readFile(__dirname	+	'/file.txt',	function	(err,	data)	{
					res.end(data);
			});
	});

VS
	http.createServer(function	(req,	res)	{
			var	stream	=	fs.createReadStream(__dirname	+	'/file.txt');
			stream.pipe(res);
	});
CHILD	PROCESS
	var	cp	=	require('child_process');
	cp.spawn(command,	[args],	[options]);
	cp.exec(command,	[options],	callback);
	cp.execFile(file,	[args],	[options],	[callback]);
	cp.fork(modulePath,	[args],	[options]);
	child.stdin		//	Writable	stream
	child.stdout	//	Readable	stream
	child.stderr	//	Readable	stream
	//	Sync,	used	with	'forked'	process	only
	child.send(message);
	child.on('message',	callback);
CLUSTER
	var	cluster	=	require('cluster'),
					http	=	require('http'),
					numCPUs	=	require('os').cpus().length;
	if	(cluster.isMaster)	{
			for	(var	i	=	0;	i	<	numCPUs;	i++)	{
					cluster.fork();
			}
	}	else	{
			http.createServer(function	(req,	res)	{
					res.writeHead(200,	{'Content-Type':	'text/plain'});
					res.end('Hello	World!');
			}).listen(3000);
	}
PROTOTYPING
MEAN	STACK
MongoDB	+	Express	+	AngularJS	+	Node.js

	

mean.io
EXPRESS
Middleware	system
Router
Templating	(Jade,	EJS)
	npm	install	-g	express
	express	appname
SETTING	UP
	var	express	=	require('express'),
					app	=	express();
	app.set('port',	process.env.PORT	||	3000);
	app.set('view	engine',	'jade');
	app.set('views',	__dirname	+	'/views');
	app.use(express.bodyParser());
	app.use(express.methodOverride());
	app.use(app.router);
	app.use(express.static(path.join(__dirname,	'public')));
	app.listen(app.get('port'));
MIDDLEWARES
	app.use(function	(req,	res,	next)	{
		//	Do	something...
		next();	//	Call	next	middleware
	});
var	middleware	=	function	(req,	res,	next)	{
		//	Do	something...
		next();	//	Call	next	middleware
};
ROUTES
	app.get('/',	function	(req,	res)	{
			res.render('index');
	});
	app.get('/user',	middleware,	function	(req,	res)	{
			res.render('user');	//	Render	page	for	authorized	users	only
	});
ERROR	HANDLING
CUSTOM	ERRORS
	var	AuthError	=	function	(msg)	{
			Error.call(this);
			Error.captureStackTrace(this,	arguments.callee);
			this.message	=	msg;
			this.name	=	'AuthError';
	};
	AuthError.prototype.__proto__	=	Error.prototype;
ERROR	HANDLING
ERROR	MIDDLEWARE
	app.use(function	(err,	req,	res,	next)	{
			if	(err.name	==	'AuthError')	{
					res.send(401,	{error:	err.message});
			}
	});
	var	middleware	=	function	(req,	res,	next)	{
			if	(req.body.password	!=	'password')	{
					return	next(new	AuthError('Unauthorized'));
			}
			next();
	};
MONGODB
Document-Oriented	BSON	data	storage
+	Mongoose	ODM
	var	mongoose	=	require('mongoose');
	mongoose.connect('mongodb://localhost/dbname');
MONGOOSE	SCHEMAS
	var	Schema	=	require('mongoose').Schema;
	var	UserSchema	=	new	Schema({
			username:	{
					type:	String,
					unique:	true,
					required:	true
			},
			comments:	[{body:	String,	date:	Date}],
			modified:	{
					type:	Date,
					default:	Date.now
			},
			role:	String
	});
	var	User	=	mongoose.model('User',	UserSchema);
CRUD
	var	user	=	new	User(data);
	user.save(function	(err,	user)	{});
	User.find(function	(err,	users)	{});
	User.find({role:	'Moderator'},	function	(err,	users)	{});
	User.findOne({username:	'user'},	function	(err,	user)	{});
	User.findById(id,	function	(err,	user)	{
			user.remove(function	(err)	{});
	});
JSON/REST	API
	app.post('/books',	function	(req,	res,	next)	{
			var	book	=	new	Book(req.body);
			book.save(function	(err,	book)	{
					if	(err)	return	next(new	Error('Server	error'));
					res.send(200,	{book:	book});
			});
	});
	app.get('/books/:id',	function	(req,	res,	next)	{
			Book.findById(req.params.id,	function	(err,	book)	{
					if	(err)	return	next(new	Error('Server	error'));
					if	(!book)	return	res.send(404,	{error:	'Not	found'});
					res.send(200,	{book:	book});
			});
	});
	//	...
REAL-TIME
SOCKET.IO
	var	io	=	require('socket.io').listen(80);
	io.sockets.on('connection',	function	(socket)	{
			socket.on('my	event',	function	(data)	{});
			socket.emit('another	event',	data);
	});
DEMO	TIME
WHAT	ELSE?
Forever
Nodemon
Node-Inspect
Meteor
JXcore
CLI
Front-end	tools
LINKS
nodejs.org/api
github.com/substack/stream-handbook
nodestreams.com
howtonode.org
nodeschool.io
github.com/roman01la/RTVideo

More Related Content

PDF
All aboard the NodeJS Express
David Boyer
 
PPTX
Node.js in a heterogeneous system
Alexey (Mr_Mig) Migutsky
 
PPTX
Herramientas front
borya09
 
PDF
Non-blocking I/O, Event loops and node.js
Marcus Frödin
 
PDF
Docker
Kamil Grabowski
 
PDF
Java/Spring과 Node.js의 공존 시즌2
동수 장
 
PDF
Ansible
gnosek
 
PDF
Warsztaty ansible
gnosek
 
All aboard the NodeJS Express
David Boyer
 
Node.js in a heterogeneous system
Alexey (Mr_Mig) Migutsky
 
Herramientas front
borya09
 
Non-blocking I/O, Event loops and node.js
Marcus Frödin
 
Java/Spring과 Node.js의 공존 시즌2
동수 장
 
Ansible
gnosek
 
Warsztaty ansible
gnosek
 

What's hot (20)

PPT
Building your first Node app with Connect & Express
Christian Joudrey
 
PDF
Node.js in a heterogeneous system
GeeksLab Odessa
 
PDF
Webconf nodejs-production-architecture
Ben Lin
 
PDF
Nodejs vatsal shah
Vatsal N Shah
 
PDF
Introduction to Node.js: What, why and how?
Christian Joudrey
 
PDF
[Js hcm] Deploying node.js with Forever.js and nginx
Nicolas Embleton
 
PDF
Node ppt
Tamil Selvan R S
 
KEY
Writing robust Node.js applications
Tom Croucher
 
PDF
(WS14) Sasa Matijasic - Node.js i "novi" web
Web::Strategija
 
PPTX
Introduction to Node.js
Vikash Singh
 
KEY
Building HTTP API's with NodeJS and MongoDB
donnfelker
 
PPTX
introduction to node.js
orkaplan
 
PPT
RESTful API In Node Js using Express
Jeetendra singh
 
PPTX
Node js introduction
Joseph de Castelnau
 
PDF
Npm: beyond 'npm i'
Pieter Herroelen
 
PDF
Software Tests with MongoDB
MongoDB
 
KEY
Introduction to node.js
jacekbecela
 
PPTX
Introduction to Node js
Akshay Mathur
 
PPTX
Java script at backend nodejs
Amit Thakkar
 
PPTX
Introduction to node.js GDD
Sudar Muthu
 
Building your first Node app with Connect & Express
Christian Joudrey
 
Node.js in a heterogeneous system
GeeksLab Odessa
 
Webconf nodejs-production-architecture
Ben Lin
 
Nodejs vatsal shah
Vatsal N Shah
 
Introduction to Node.js: What, why and how?
Christian Joudrey
 
[Js hcm] Deploying node.js with Forever.js and nginx
Nicolas Embleton
 
Writing robust Node.js applications
Tom Croucher
 
(WS14) Sasa Matijasic - Node.js i "novi" web
Web::Strategija
 
Introduction to Node.js
Vikash Singh
 
Building HTTP API's with NodeJS and MongoDB
donnfelker
 
introduction to node.js
orkaplan
 
RESTful API In Node Js using Express
Jeetendra singh
 
Node js introduction
Joseph de Castelnau
 
Npm: beyond 'npm i'
Pieter Herroelen
 
Software Tests with MongoDB
MongoDB
 
Introduction to node.js
jacekbecela
 
Introduction to Node js
Akshay Mathur
 
Java script at backend nodejs
Amit Thakkar
 
Introduction to node.js GDD
Sudar Muthu
 
Ad

Similar to Node.js :: Introduction — Part 2 (20)

PDF
Server Side Apocalypse, JS
Md. Sohel Rana
 
PPT
Ferrara Linux Day 2011
Gianluca Padovani
 
PPTX
Introduction to node.js By Ahmed Assaf
Ahmed Assaf
 
PDF
Node intro
Vishal Sharma
 
PDF
Introduction to Node.js
Somkiat Puisungnoen
 
PDF
Node js introduction
Alex Su
 
PDF
Node.js - async for the rest of us.
Mike Brevoort
 
PPTX
Nodejs
dssprakash
 
PPTX
Node js training (1)
Ashish Gupta
 
PDF
Nodejs first class
Fin Chen
 
PDF
Jaap : node, npm & grunt
Bertrand Chevrier
 
PPTX
Building Web Apps with Express
Aaron Stannard
 
PDF
Node.jsやってみた
Yoshihiko Uchida
 
ODP
Introduce about Nodejs - duyetdev.com
Van-Duyet Le
 
PPTX
NodeJS
Alok Guha
 
PDF
OSDC.no 2015 introduction to node.js workshop
leffen
 
PDF
Node.js and Google Cloud
Paulo Pires
 
PDF
Automação do físico ao NetSecDevOps
Raul Leite
 
PPT
Node js beginner
Sureshreddy Nalimela
 
PDF
Grunt & Front-end Workflow
Pagepro
 
Server Side Apocalypse, JS
Md. Sohel Rana
 
Ferrara Linux Day 2011
Gianluca Padovani
 
Introduction to node.js By Ahmed Assaf
Ahmed Assaf
 
Node intro
Vishal Sharma
 
Introduction to Node.js
Somkiat Puisungnoen
 
Node js introduction
Alex Su
 
Node.js - async for the rest of us.
Mike Brevoort
 
Nodejs
dssprakash
 
Node js training (1)
Ashish Gupta
 
Nodejs first class
Fin Chen
 
Jaap : node, npm & grunt
Bertrand Chevrier
 
Building Web Apps with Express
Aaron Stannard
 
Node.jsやってみた
Yoshihiko Uchida
 
Introduce about Nodejs - duyetdev.com
Van-Duyet Le
 
NodeJS
Alok Guha
 
OSDC.no 2015 introduction to node.js workshop
leffen
 
Node.js and Google Cloud
Paulo Pires
 
Automação do físico ao NetSecDevOps
Raul Leite
 
Node js beginner
Sureshreddy Nalimela
 
Grunt & Front-end Workflow
Pagepro
 
Ad

Recently uploaded (20)

PPTX
What-is-the-World-Wide-Web -- Introduction
tonifi9488
 
PDF
NewMind AI Weekly Chronicles - July'25 - Week IV
NewMind AI
 
PPTX
Simple and concise overview about Quantum computing..pptx
mughal641
 
PDF
Software Development Methodologies in 2025
KodekX
 
PDF
Oracle AI Vector Search- Getting Started and what's new in 2025- AIOUG Yatra ...
Sandesh Rao
 
PDF
Structs to JSON: How Go Powers REST APIs
Emily Achieng
 
PDF
Automating ArcGIS Content Discovery with FME: A Real World Use Case
Safe Software
 
PDF
AI Unleashed - Shaping the Future -Starting Today - AIOUG Yatra 2025 - For Co...
Sandesh Rao
 
PDF
Tea4chat - another LLM Project by Kerem Atam
a0m0rajab1
 
PPTX
cloud computing vai.pptx for the project
vaibhavdobariyal79
 
PPTX
The Future of AI & Machine Learning.pptx
pritsen4700
 
PDF
OFFOFFBOX™ – A New Era for African Film | Startup Presentation
ambaicciwalkerbrian
 
PDF
AI-Cloud-Business-Management-Platforms-The-Key-to-Efficiency-Growth.pdf
Artjoker Software Development Company
 
PDF
Make GenAI investments go further with the Dell AI Factory
Principled Technologies
 
PDF
GDG Cloud Munich - Intro - Luiz Carneiro - #BuildWithAI - July - Abdel.pdf
Luiz Carneiro
 
PDF
Orbitly Pitch Deck|A Mission-Driven Platform for Side Project Collaboration (...
zz41354899
 
PPTX
Agile Chennai 18-19 July 2025 Ideathon | AI Powered Microfinance Literacy Gui...
AgileNetwork
 
PDF
Presentation about Hardware and Software in Computer
snehamodhawadiya
 
PDF
SparkLabs Primer on Artificial Intelligence 2025
SparkLabs Group
 
PDF
Security features in Dell, HP, and Lenovo PC systems: A research-based compar...
Principled Technologies
 
What-is-the-World-Wide-Web -- Introduction
tonifi9488
 
NewMind AI Weekly Chronicles - July'25 - Week IV
NewMind AI
 
Simple and concise overview about Quantum computing..pptx
mughal641
 
Software Development Methodologies in 2025
KodekX
 
Oracle AI Vector Search- Getting Started and what's new in 2025- AIOUG Yatra ...
Sandesh Rao
 
Structs to JSON: How Go Powers REST APIs
Emily Achieng
 
Automating ArcGIS Content Discovery with FME: A Real World Use Case
Safe Software
 
AI Unleashed - Shaping the Future -Starting Today - AIOUG Yatra 2025 - For Co...
Sandesh Rao
 
Tea4chat - another LLM Project by Kerem Atam
a0m0rajab1
 
cloud computing vai.pptx for the project
vaibhavdobariyal79
 
The Future of AI & Machine Learning.pptx
pritsen4700
 
OFFOFFBOX™ – A New Era for African Film | Startup Presentation
ambaicciwalkerbrian
 
AI-Cloud-Business-Management-Platforms-The-Key-to-Efficiency-Growth.pdf
Artjoker Software Development Company
 
Make GenAI investments go further with the Dell AI Factory
Principled Technologies
 
GDG Cloud Munich - Intro - Luiz Carneiro - #BuildWithAI - July - Abdel.pdf
Luiz Carneiro
 
Orbitly Pitch Deck|A Mission-Driven Platform for Side Project Collaboration (...
zz41354899
 
Agile Chennai 18-19 July 2025 Ideathon | AI Powered Microfinance Literacy Gui...
AgileNetwork
 
Presentation about Hardware and Software in Computer
snehamodhawadiya
 
SparkLabs Primer on Artificial Intelligence 2025
SparkLabs Group
 
Security features in Dell, HP, and Lenovo PC systems: A research-based compar...
Principled Technologies
 

Node.js :: Introduction — Part 2