SlideShare ist ein Scribd-Unternehmen logo
1 von 24
Basic Concept of Node.Js &
NPM
Bhargav Anadkat
Twitter @bhargavlalo
Facebook bhargav.anadkat
WHAT TO EXPECT AHEAD….
 Introduction Video Song
 What is NodeJS?
 Reasons to use NodeJS
 Confusing With NodeJS
 Who uses NodeJS?
 NodeJs Event Loop
 Introduction to NPM (Node Package
Manager)
 Some good modules
 List of NodeJS Core Packages
 NodeJS Installation
 Command Line Intro & Node
Command
 First Hello World Example
 Node.js Ecosystem
 Blocking I/O and Non Blocking
I/O
 Non Blocking Example
 Create Package Dependency
 Example TCP Server
 Example MongoDB
 Example Twitter Streaming
 Advance Example
 Question/Answer
 Thank You
WHAT IS NODEJS?
 NodeJS is an open source, cross platform runtime
environment for server side and networking
application.
 Basically, NodeJS is server side programming
language like PHP, C#, Python etc.
 In ‘Node.js’ , ‘.js’ doesn’t mean that its solely written
JavaScript. It is 40% JS and 60% C++.
 NodeJS is cross platform. So it runs well on Linux
systems, Mac, can also run on Windows systems.
 It provided an event driven architecture and a non
blocking I/O that optimize and scalability. These
technology uses for real time application
#2 WHAT IS NODEJS?
 It used Google JavaScript V8 Engine to Execute
Code.
 V8 is an open source JavaScript engine developed
by Google. Its written in C++ and is used in Google
Chrome Browser.
 NodeJS invented in 2009 by Ryan Dahl.
 NodeJS is popular in development because front &
backend side both uses JavaScript Code.
‘Node's goal is to provide an easy way to build
scalable network programs’ - (from nodejs.org!)
REASONS TO USE NODEJS
 It is Open Source and cross platform. It runs well on
Linux systems, Mac, can also run on Windows
systems.
 It is fast.
 Node.js wins with Ubiquity
 Node.js wins with Data Streaming
 Node.js wins with Database Queries
 Real Time web applications Support
 Node.js –Effective Tooling with NPM
 Many more…
CONFUSING WITH NODEJS
 Programs for Node.js are written in JavaScript but not
in the same JavaScript we are use to. There is no DOM
implementation provided by Node.js, i.e. you can not
do this:
var element = document.getElementById(“elementId”);
 Everything inside Node.js runs in a single-thread.
WHO USES NODEJS?
WHO IS USING NODE.JS IN PRODUCTION?
 Yahoo! : iPad App Livestand uses Yahoo!
Manhattan framework which is based on Node.js.
 LinkedIn : LinkedIn uses a combination of Node.js
and MongoDB for its mobile platform. iOS and
Android apps are based on it.
 eBay : Uses Node.js along with ql.io to help
application developers in improving eBay’s end
user experience.
 Dow Jones : The WSJ Social front-end is written
completely in Node.js, using Express.js, and many
other modules.
 Complete list can be found at:
https://github.com/joyent/node/wiki/Projects,-
Applications,-and-Companies-Using-Node
NODEJS EVENT LOOP
 Event-loops are the core of event-driven programming,
almost all the UI programs use event-loops to track the
user event, for example: Clicks, Ajax Requests etc.
Client
Event loop
(main thread)
C++
Threadpool
(worker
threads)
Clients send HTTP requests
to Node.js server
An Event-loop is woken up by OS,
passes request and response objects
to the thread-pool
Long-running jobs run
on worker threads
Response is sent
back to main thread
via callback
Event loop returns
result to client
INTRODUCTION TO NPM (NODE PACKAGE
MANAGER)
 npm is the package manager for Node.js and JavaScript
 We can create public & private module
 Find all modules on NPM website : https://www.npmjs.com/
 There 5.5 Lacks+ packages available So in world its largest JavaScript
package manager.
 Below are some most popular packages which used in many projects.
 lodash
 express
 request
 react
 debug
 bluebird
 async
 chalk
 commander
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
LIST OF NODEJS CORE PACKAGES
 Assertion
 Testing
 File System
 Path
 Debugger
 Query Strings
 Events
 String
 TLS/SSL
 URL
 Buffer
 HTTP/HTTPS
 C/C++ Addons
 Crypto
 Punycode
 Domain
 Stream
 Decoder
 TTY
 Utilities
 Child Processes
 OS
 Cluster
 Process
 DNS
 REPL
 Core
 Timers
 UDP/Datagram
 VM
NODEJS INSTALLATION
 Download at https://nodejs.org/en/download/
 Available in installable file and source code.
 It is easy to install so you can start start developing
today on any platform.
 In latest version npm (Node Package Manager)
already available inside it so no need to download
npm separately.
COMMAND LINE INTRO & NODE COMMAND
 Windows platform, Open command prompt (cmd)
 Below are some node basic command introduction
 node : open node editor and write and execute
node JavaScript code there.
 node -v : check the node version
 node -h : help command
 node [packagename]: it runs your package
(module)
FIRST HELLO WORLD EXAMPLE
 Hello world example code you can found on below
github link:
https://github.com/bhargavlalo/ba-nodejs-
example/blob/master/1-helloworld/1-helloworld.js
NODE.JS ECOSYSTEM
 Node.js heavily relies on modules, in previous
examples require keyword loaded the http
modules.
 Creating a module is easy, just put your JavaScript
code in a separate js file and include it in your code
by using keyword require, like:
var modulex = require(‘./modulex’);
 Libraries in Node.js are called packages and they
can be installed by typing
npm install “package_name”; //package should be
available in npm registry @ nmpjs.org
 NPM (Node Package Manager) comes bundled
with Node.js installation.
BLOCKING I/O AND 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!
NON-BLOCKING EXAMPLE
 Nonblocking example code:
https://github.com/bhargavlalo/ba-nodejs-
example/blob/master/4-nonblocking/nonblocking.js
EXAMPLE TCP SERVER
 Here is an example of a simple TCP server which
listens on port 6000 and echoes whatever you send
it:
var net = require('net');
net.createServer(function (socket) {
socket.write("Echo serverrn");
socket.pipe(socket); }).listen(6000, "127.0.0.1");
EXAMPLE 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);
});
});
EXAMPLE TWITTER STREAMING
 Install nTwitter module using npm:
Npm install ntwitter
 Code:
var twitter = require('ntwitter');
var twit = new twitter({
consumer_key: ‘c_key’,
consumer_secret: ‘c_secret’,
access_token_key: ‘token_key’,
access_token_secret: ‘token_secret’});
twit.stream('statuses/sample', function(stream) {
stream.on('data', function (data) {
console.log(data); });
});
ADVANCE EXAMPLES
 Some advance examples created on my github
repo.
 Below is the link
https://github.com/bhargavlalo/ba-nodejs-example
 Examples
 Desktop Application
 HTML Render
 Node Mailer
Question & Answer
Thank You
console.log(“Bhargav Anadkat”);

Weitere ähnliche Inhalte

Was ist angesagt?

Was ist angesagt? (20)

Introduction to Node.js
Introduction to Node.jsIntroduction to Node.js
Introduction to Node.js
 
Introduction to node.js
Introduction to node.jsIntroduction to node.js
Introduction to node.js
 
Introduction to Node.js
Introduction to Node.jsIntroduction to Node.js
Introduction to Node.js
 
Nodejs presentation
Nodejs presentationNodejs presentation
Nodejs presentation
 
What is Node.js | Node.js Tutorial for Beginners | Node.js Modules | Node.js ...
What is Node.js | Node.js Tutorial for Beginners | Node.js Modules | Node.js ...What is Node.js | Node.js Tutorial for Beginners | Node.js Modules | Node.js ...
What is Node.js | Node.js Tutorial for Beginners | Node.js Modules | Node.js ...
 
NestJS
NestJSNestJS
NestJS
 
Node js introduction
Node js introductionNode js introduction
Node js introduction
 
Introduction to Node js
Introduction to Node jsIntroduction to Node js
Introduction to Node js
 
Introduction to Node JS.pdf
Introduction to Node JS.pdfIntroduction to Node JS.pdf
Introduction to Node JS.pdf
 
Node.Js: Basics Concepts and Introduction
Node.Js: Basics Concepts and Introduction Node.Js: Basics Concepts and Introduction
Node.Js: Basics Concepts and Introduction
 
Introduction to Node.js
Introduction to Node.jsIntroduction to Node.js
Introduction to Node.js
 
Express js
Express jsExpress js
Express js
 
Node.js Express Tutorial | Node.js Tutorial For Beginners | Node.js + Expres...
Node.js Express Tutorial | Node.js Tutorial For Beginners | Node.js +  Expres...Node.js Express Tutorial | Node.js Tutorial For Beginners | Node.js +  Expres...
Node.js Express Tutorial | Node.js Tutorial For Beginners | Node.js + Expres...
 
MEAN Stack
MEAN StackMEAN Stack
MEAN Stack
 
Nodejs vatsal shah
Nodejs vatsal shahNodejs vatsal shah
Nodejs vatsal shah
 
Node js crash course session 1
Node js crash course   session 1Node js crash course   session 1
Node js crash course session 1
 
Workshop 4: NodeJS. Express Framework & MongoDB.
Workshop 4: NodeJS. Express Framework & MongoDB.Workshop 4: NodeJS. Express Framework & MongoDB.
Workshop 4: NodeJS. Express Framework & MongoDB.
 
Node js for beginners
Node js for beginnersNode js for beginners
Node js for beginners
 
Node.js
Node.jsNode.js
Node.js
 
Express JS
Express JSExpress JS
Express JS
 

Ähnlich wie Basic Concept of Node.js & NPM

Node.js.pdf
Node.js.pdfNode.js.pdf
Node.js.pdf
gulfam ali
 
Node Js Non-blocking or asynchronous Blocking or synchronous.pdf
Node Js Non-blocking or asynchronous  Blocking or synchronous.pdfNode Js Non-blocking or asynchronous  Blocking or synchronous.pdf
Node Js Non-blocking or asynchronous Blocking or synchronous.pdf
DarshanaMallick
 
Node.js Web Development .pdf
Node.js Web Development .pdfNode.js Web Development .pdf
Node.js Web Development .pdf
Abanti Aazmin
 

Ähnlich wie Basic Concept of Node.js & NPM (20)

Nodejs
NodejsNodejs
Nodejs
 
Node J pdf.docx
Node J pdf.docxNode J pdf.docx
Node J pdf.docx
 
Node J pdf.docx
Node J pdf.docxNode J pdf.docx
Node J pdf.docx
 
Node js (runtime environment + js library) platform
Node js (runtime environment + js library) platformNode js (runtime environment + js library) platform
Node js (runtime environment + js library) platform
 
All You Need to Know About Using Node.pdf
All You Need to Know About Using Node.pdfAll You Need to Know About Using Node.pdf
All You Need to Know About Using Node.pdf
 
02 Node introduction
02 Node introduction02 Node introduction
02 Node introduction
 
Node js presentation
Node js presentationNode js presentation
Node js presentation
 
Introduction to node.js By Ahmed Assaf
Introduction to node.js  By Ahmed AssafIntroduction to node.js  By Ahmed Assaf
Introduction to node.js By Ahmed Assaf
 
Node.js.pdf
Node.js.pdfNode.js.pdf
Node.js.pdf
 
Halton Software Peer 2 Peer Meetup #10
Halton Software Peer 2 Peer Meetup #10Halton Software Peer 2 Peer Meetup #10
Halton Software Peer 2 Peer Meetup #10
 
Node Js Non-blocking or asynchronous Blocking or synchronous.pdf
Node Js Non-blocking or asynchronous  Blocking or synchronous.pdfNode Js Non-blocking or asynchronous  Blocking or synchronous.pdf
Node Js Non-blocking or asynchronous Blocking or synchronous.pdf
 
Node.js Web Development .pdf
Node.js Web Development .pdfNode.js Web Development .pdf
Node.js Web Development .pdf
 
Kalp Corporate Node JS Perfect Guide
Kalp Corporate Node JS Perfect GuideKalp Corporate Node JS Perfect Guide
Kalp Corporate Node JS Perfect Guide
 
Mastering node.js, part 1 - introduction
Mastering node.js, part 1 - introductionMastering node.js, part 1 - introduction
Mastering node.js, part 1 - introduction
 
Introduction to NodeJS JSX is an extended Javascript based language used by R...
Introduction to NodeJS JSX is an extended Javascript based language used by R...Introduction to NodeJS JSX is an extended Javascript based language used by R...
Introduction to NodeJS JSX is an extended Javascript based language used by R...
 
Node JS Express : Steps to Create Restful Web App
Node JS Express : Steps to Create Restful Web AppNode JS Express : Steps to Create Restful Web App
Node JS Express : Steps to Create Restful Web App
 
Node js
Node jsNode js
Node js
 
Create Restful Web Application With Node.js Express Framework
Create Restful Web Application With Node.js Express FrameworkCreate Restful Web Application With Node.js Express Framework
Create Restful Web Application With Node.js Express Framework
 
Nodejs
NodejsNodejs
Nodejs
 
Node JS Express: Steps to Create Restful Web App
Node JS Express: Steps to Create Restful Web AppNode JS Express: Steps to Create Restful Web App
Node JS Express: Steps to Create Restful Web App
 

Kürzlich hochgeladen

Kürzlich hochgeladen (20)

Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingRepurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
 
Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
HTML Injection Attacks: Impact and Mitigation Strategies
HTML Injection Attacks: Impact and Mitigation StrategiesHTML Injection Attacks: Impact and Mitigation Strategies
HTML Injection Attacks: Impact and Mitigation Strategies
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of Terraform
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
Deploy with confidence: VMware Cloud Foundation 5.1 on next gen Dell PowerEdg...
Deploy with confidence: VMware Cloud Foundation 5.1 on next gen Dell PowerEdg...Deploy with confidence: VMware Cloud Foundation 5.1 on next gen Dell PowerEdg...
Deploy with confidence: VMware Cloud Foundation 5.1 on next gen Dell PowerEdg...
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
 
Manulife - Insurer Innovation Award 2024
Manulife - Insurer Innovation Award 2024Manulife - Insurer Innovation Award 2024
Manulife - Insurer Innovation Award 2024
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024
 
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
 

Basic Concept of Node.js & NPM

  • 1. Basic Concept of Node.Js & NPM Bhargav Anadkat Twitter @bhargavlalo Facebook bhargav.anadkat
  • 2. WHAT TO EXPECT AHEAD….  Introduction Video Song  What is NodeJS?  Reasons to use NodeJS  Confusing With NodeJS  Who uses NodeJS?  NodeJs Event Loop  Introduction to NPM (Node Package Manager)  Some good modules  List of NodeJS Core Packages  NodeJS Installation  Command Line Intro & Node Command  First Hello World Example  Node.js Ecosystem  Blocking I/O and Non Blocking I/O  Non Blocking Example  Create Package Dependency  Example TCP Server  Example MongoDB  Example Twitter Streaming  Advance Example  Question/Answer  Thank You
  • 3. WHAT IS NODEJS?  NodeJS is an open source, cross platform runtime environment for server side and networking application.  Basically, NodeJS is server side programming language like PHP, C#, Python etc.  In ‘Node.js’ , ‘.js’ doesn’t mean that its solely written JavaScript. It is 40% JS and 60% C++.  NodeJS is cross platform. So it runs well on Linux systems, Mac, can also run on Windows systems.  It provided an event driven architecture and a non blocking I/O that optimize and scalability. These technology uses for real time application
  • 4. #2 WHAT IS NODEJS?  It used Google JavaScript V8 Engine to Execute Code.  V8 is an open source JavaScript engine developed by Google. Its written in C++ and is used in Google Chrome Browser.  NodeJS invented in 2009 by Ryan Dahl.  NodeJS is popular in development because front & backend side both uses JavaScript Code. ‘Node's goal is to provide an easy way to build scalable network programs’ - (from nodejs.org!)
  • 5. REASONS TO USE NODEJS  It is Open Source and cross platform. It runs well on Linux systems, Mac, can also run on Windows systems.  It is fast.  Node.js wins with Ubiquity  Node.js wins with Data Streaming  Node.js wins with Database Queries  Real Time web applications Support  Node.js –Effective Tooling with NPM  Many more…
  • 6. CONFUSING WITH NODEJS  Programs for Node.js are written in JavaScript but not in the same JavaScript we are use to. There is no DOM implementation provided by Node.js, i.e. you can not do this: var element = document.getElementById(“elementId”);  Everything inside Node.js runs in a single-thread.
  • 8. WHO IS USING NODE.JS IN PRODUCTION?  Yahoo! : iPad App Livestand uses Yahoo! Manhattan framework which is based on Node.js.  LinkedIn : LinkedIn uses a combination of Node.js and MongoDB for its mobile platform. iOS and Android apps are based on it.  eBay : Uses Node.js along with ql.io to help application developers in improving eBay’s end user experience.  Dow Jones : The WSJ Social front-end is written completely in Node.js, using Express.js, and many other modules.  Complete list can be found at: https://github.com/joyent/node/wiki/Projects,- Applications,-and-Companies-Using-Node
  • 9. NODEJS EVENT LOOP  Event-loops are the core of event-driven programming, almost all the UI programs use event-loops to track the user event, for example: Clicks, Ajax Requests etc. Client Event loop (main thread) C++ Threadpool (worker threads) Clients send HTTP requests to Node.js server An Event-loop is woken up by OS, passes request and response objects to the thread-pool Long-running jobs run on worker threads Response is sent back to main thread via callback Event loop returns result to client
  • 10. INTRODUCTION TO NPM (NODE PACKAGE MANAGER)  npm is the package manager for Node.js and JavaScript  We can create public & private module  Find all modules on NPM website : https://www.npmjs.com/  There 5.5 Lacks+ packages available So in world its largest JavaScript package manager.  Below are some most popular packages which used in many projects.  lodash  express  request  react  debug  bluebird  async  chalk  commander
  • 11. 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
  • 12. LIST OF NODEJS CORE PACKAGES  Assertion  Testing  File System  Path  Debugger  Query Strings  Events  String  TLS/SSL  URL  Buffer  HTTP/HTTPS  C/C++ Addons  Crypto  Punycode  Domain  Stream  Decoder  TTY  Utilities  Child Processes  OS  Cluster  Process  DNS  REPL  Core  Timers  UDP/Datagram  VM
  • 13. NODEJS INSTALLATION  Download at https://nodejs.org/en/download/  Available in installable file and source code.  It is easy to install so you can start start developing today on any platform.  In latest version npm (Node Package Manager) already available inside it so no need to download npm separately.
  • 14. COMMAND LINE INTRO & NODE COMMAND  Windows platform, Open command prompt (cmd)  Below are some node basic command introduction  node : open node editor and write and execute node JavaScript code there.  node -v : check the node version  node -h : help command  node [packagename]: it runs your package (module)
  • 15. FIRST HELLO WORLD EXAMPLE  Hello world example code you can found on below github link: https://github.com/bhargavlalo/ba-nodejs- example/blob/master/1-helloworld/1-helloworld.js
  • 16. NODE.JS ECOSYSTEM  Node.js heavily relies on modules, in previous examples require keyword loaded the http modules.  Creating a module is easy, just put your JavaScript code in a separate js file and include it in your code by using keyword require, like: var modulex = require(‘./modulex’);  Libraries in Node.js are called packages and they can be installed by typing npm install “package_name”; //package should be available in npm registry @ nmpjs.org  NPM (Node Package Manager) comes bundled with Node.js installation.
  • 17. BLOCKING I/O AND 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!
  • 18. NON-BLOCKING EXAMPLE  Nonblocking example code: https://github.com/bhargavlalo/ba-nodejs- example/blob/master/4-nonblocking/nonblocking.js
  • 19. EXAMPLE TCP SERVER  Here is an example of a simple TCP server which listens on port 6000 and echoes whatever you send it: var net = require('net'); net.createServer(function (socket) { socket.write("Echo serverrn"); socket.pipe(socket); }).listen(6000, "127.0.0.1");
  • 20. EXAMPLE 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); }); });
  • 21. EXAMPLE TWITTER STREAMING  Install nTwitter module using npm: Npm install ntwitter  Code: var twitter = require('ntwitter'); var twit = new twitter({ consumer_key: ‘c_key’, consumer_secret: ‘c_secret’, access_token_key: ‘token_key’, access_token_secret: ‘token_secret’}); twit.stream('statuses/sample', function(stream) { stream.on('data', function (data) { console.log(data); }); });
  • 22. ADVANCE EXAMPLES  Some advance examples created on my github repo.  Below is the link https://github.com/bhargavlalo/ba-nodejs-example  Examples  Desktop Application  HTML Render  Node Mailer