NodeJS is an open source, cross platform run time environment for server side and networking application. NodeJS is popular in development because front & back end side both uses JavaScript Code.
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