nodejs
DESCRIPTION
Introduction to Node.jsTRANSCRIPT
![Page 1: Nodejs](https://reader034.vdocuments.site/reader034/viewer/2022052619/554f5f25b4c905c8088b48b9/html5/thumbnails/1.jpg)
Author: Bhushan PatilDate: 08 Mar 2014
![Page 2: Nodejs](https://reader034.vdocuments.site/reader034/viewer/2022052619/554f5f25b4c905c8088b48b9/html5/thumbnails/2.jpg)
What is Node.js?
![Page 3: Nodejs](https://reader034.vdocuments.site/reader034/viewer/2022052619/554f5f25b4c905c8088b48b9/html5/thumbnails/3.jpg)
Node.js or nodejs or “node” simply is “server side JavaScript”
Node.js is software platform for scalable server-side and networking applications.
Allows you to build scalable network applications using JavaScript on the server-side.
What is Node.js?
Node.js V8 JavaScript
Runtime
![Page 4: Nodejs](https://reader034.vdocuments.site/reader034/viewer/2022052619/554f5f25b4c905c8088b48b9/html5/thumbnails/4.jpg)
The V8 JavaScript Engine is an open source JavaScript engine developed by Google for the Google Chrome web browser.
V8 compiles JavaScript to native machine code (IA-32, x86-64, ARM, or MIPS ISAs) before executing it, instead of more traditional techniques such as executing bytecode or interpreting it.
Google v8 JavaScript Engine
![Page 5: Nodejs](https://reader034.vdocuments.site/reader034/viewer/2022052619/554f5f25b4c905c8088b48b9/html5/thumbnails/5.jpg)
“JavaScript has certain characteristics that make it very different than other dynamic languages, namely that it has no concept of threads. Its model of concurrency is completely based around events.” - Ryan Dahl (Author node.js)
Why Javascript?
![Page 6: Nodejs](https://reader034.vdocuments.site/reader034/viewer/2022052619/554f5f25b4c905c8088b48b9/html5/thumbnails/6.jpg)
A Web Framework For Beginners – it’s very low level Multi-threaded – You can think of it as single
threaded server
What is node.js not?
![Page 7: Nodejs](https://reader034.vdocuments.site/reader034/viewer/2022052619/554f5f25b4c905c8088b48b9/html5/thumbnails/7.jpg)
Node.js Vs Traditional server side scripting
![Page 8: Nodejs](https://reader034.vdocuments.site/reader034/viewer/2022052619/554f5f25b4c905c8088b48b9/html5/thumbnails/8.jpg)
Node js is for real-time web ◦ Node shines in real-time web applications
employing push technology over websockets Vs stateless-web based on the stateless request-response paradigm.
Uses event-driven asynchronous callbacks
Handles concurrency very well than other traditional web server
Difference between node.js and traditional server side scripting
![Page 9: Nodejs](https://reader034.vdocuments.site/reader034/viewer/2022052619/554f5f25b4c905c8088b48b9/html5/thumbnails/9.jpg)
Event-driven asynchronous callbacks
var result = database.query("SELECT * FROM hugetable"); console.log("Hello World");
database.query("SELECT * FROM hugetable", function(rows) { var result = rows; }); console.log("Hello World");
Traditional way
Vs
Asynchronous way
![Page 10: Nodejs](https://reader034.vdocuments.site/reader034/viewer/2022052619/554f5f25b4c905c8088b48b9/html5/thumbnails/10.jpg)
Concurrency
![Page 11: Nodejs](https://reader034.vdocuments.site/reader034/viewer/2022052619/554f5f25b4c905c8088b48b9/html5/thumbnails/11.jpg)
Performancevar i, a, b, c, max; max = 1000000000; var d = Date.now(); for (i = 0; i < max; i++) { a = 1234 + 5678 + i; b = 1234 * 5678 + i; c = 1234 / 2 + i;} console.log(Date.now() - d);
$a = null; $b = null; $c = null; $i = null;$max = 1000000000;$start = microtime(true);for ($i = 0; $i < $max; $i++) { $a = 1234 + 5678 + $i; $b = 1234 * 5678 + $i; $c = 1234 / 2 + $i;}var_dump(microtime(true) - $start);
It means PHP is 93% slower than Node.js!
Time comparison in milliseconds
![Page 12: Nodejs](https://reader034.vdocuments.site/reader034/viewer/2022052619/554f5f25b4c905c8088b48b9/html5/thumbnails/12.jpg)
What you can build?
![Page 13: Nodejs](https://reader034.vdocuments.site/reader034/viewer/2022052619/554f5f25b4c905c8088b48b9/html5/thumbnails/13.jpg)
Websocket Server - like chat application Fast File Upload Client Ad Server Any Real-Time Data Apps
What you can build with node.js?
![Page 14: Nodejs](https://reader034.vdocuments.site/reader034/viewer/2022052619/554f5f25b4c905c8088b48b9/html5/thumbnails/14.jpg)
Node.js Features
![Page 15: Nodejs](https://reader034.vdocuments.site/reader034/viewer/2022052619/554f5f25b4c905c8088b48b9/html5/thumbnails/15.jpg)
No Need of separate Server◦ it does not require a separate webserver like Apache
or Nginx or IIS. It has an inbuilt HTTP Server library which makes it possible to run a webserver without external software, and allowing more control of how the webserver works.
Non-blocking code◦ It does not execute line by line like other traditional
languages. Asynchronous programming
◦ Every function in Node.js is asynchronous. Therefore, everything that would normally block the thread is instead executed in the background.
Node.js features
![Page 16: Nodejs](https://reader034.vdocuments.site/reader034/viewer/2022052619/554f5f25b4c905c8088b48b9/html5/thumbnails/16.jpg)
Blocking vs Non-clocking code
var contents = fs.readFileSync('/etc/hosts');console.log(contents); //Stop process until complete console.log('Doing something else');
fs.readFile('/etc/hosts', function(err, contents) {console.log(contents);
});console.log('Doing something else');
Blocking code
Non-Blocking code
![Page 17: Nodejs](https://reader034.vdocuments.site/reader034/viewer/2022052619/554f5f25b4c905c8088b48b9/html5/thumbnails/17.jpg)
Asynchronous programming
var result = database.query("SELECT * FROM hugetable"); console.log("Hello World");
database.query("SELECT * FROM hugetable", function(rows) { var result = rows; }); console.log("Hello World");
Traditional way
Asynchronous way
![Page 18: Nodejs](https://reader034.vdocuments.site/reader034/viewer/2022052619/554f5f25b4c905c8088b48b9/html5/thumbnails/18.jpg)
Hello World
![Page 19: Nodejs](https://reader034.vdocuments.site/reader034/viewer/2022052619/554f5f25b4c905c8088b48b9/html5/thumbnails/19.jpg)
Example.js
var http = require('http');http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'}); res.end('Hello World\n');
}).listen(1337, '127.0.0.1');console.log('Server running at http://127.0.0.1:1337/');
% node example.jsServer running at http://127.0.0.1:1337/
If run at command prompt:
At browser: http://127.0.0.1:1337/
Hello World
![Page 20: Nodejs](https://reader034.vdocuments.site/reader034/viewer/2022052619/554f5f25b4c905c8088b48b9/html5/thumbnails/20.jpg)
NPM
![Page 21: Nodejs](https://reader034.vdocuments.site/reader034/viewer/2022052619/554f5f25b4c905c8088b48b9/html5/thumbnails/21.jpg)
npm, short for Node Package Manager, is two things: first and foremost, it is an online repository for the publishing of open-source Node.js projects; second, it is a command-line utility for interacting with said repository that aids in package installation, version management, and dependency management.
NPM - node package management
![Page 22: Nodejs](https://reader034.vdocuments.site/reader034/viewer/2022052619/554f5f25b4c905c8088b48b9/html5/thumbnails/22.jpg)
Some of the most popular NPM modules today are: Express - Express.js, a Sinatra-inspired web
development framework for Node.js, and the de-facto standard for the majority of Node.js applications out there today.
connect - Connect is an extensible HTTP server framework for Node.js, providing a collection of high performance “plugins” known as middleware; serves as a base foundation for Express.
socket.io and sockjs - Server-side component of the two most common websockets components out there today.
node.js frameworks
![Page 23: Nodejs](https://reader034.vdocuments.site/reader034/viewer/2022052619/554f5f25b4c905c8088b48b9/html5/thumbnails/23.jpg)
Jade - One of the popular templating engines, inspired by HAML, a default in Express.js.
mongo and mongojs - MongoDB wrappers to provide the API for MongoDB object databases in Node.js.
redis - Redis client library.
node.js frameworks
![Page 24: Nodejs](https://reader034.vdocuments.site/reader034/viewer/2022052619/554f5f25b4c905c8088b48b9/html5/thumbnails/24.jpg)
coffee-script - CoffeeScript compiler that allows developers to write their Node.js programs using Coffee.
underscore (lodash, lazy) – The most popular utility library in JavaScript, packaged to be used with Node.js, as well as its two counterparts, which promise better performance by taking a slightly different implementation approach.
forever - Probably the most common utility for ensuring that a given node script runs continuously. Keeps your Node.js process up in production in the face of any unexpected failures.
node.js frameworks
![Page 25: Nodejs](https://reader034.vdocuments.site/reader034/viewer/2022052619/554f5f25b4c905c8088b48b9/html5/thumbnails/25.jpg)
Example of using package(module)// Include http module, var http = require('http'), // And mysql module mysql = require("mysql");
// Create the connection. // Data is default to new mysql installation and should be changed according to your configuration. var connection = mysql.createConnection({ user: "root", password: "", database: "db_name"});
// Create the http server. http.createServer(function (request, response) { // Attach listener on end event. request.on('end', function () { // Query the database. connection.query('SELECT * FROM your_table;', function (error, rows, fields) { response.writeHead(200, { 'Content-Type': 'x-application/json' }); // Send data as JSON string. // Rows variable holds the result of the query. response.end(JSON.stringify(rows)); }); }); // Listen on the 8080 port. }).listen(8080);
![Page 26: Nodejs](https://reader034.vdocuments.site/reader034/viewer/2022052619/554f5f25b4c905c8088b48b9/html5/thumbnails/26.jpg)
Who are using node.js
![Page 27: Nodejs](https://reader034.vdocuments.site/reader034/viewer/2022052619/554f5f25b4c905c8088b48b9/html5/thumbnails/27.jpg)
And many .....
![Page 28: Nodejs](https://reader034.vdocuments.site/reader034/viewer/2022052619/554f5f25b4c905c8088b48b9/html5/thumbnails/28.jpg)
![Page 29: Nodejs](https://reader034.vdocuments.site/reader034/viewer/2022052619/554f5f25b4c905c8088b48b9/html5/thumbnails/29.jpg)
References
![Page 30: Nodejs](https://reader034.vdocuments.site/reader034/viewer/2022052619/554f5f25b4c905c8088b48b9/html5/thumbnails/30.jpg)
http://nodejs.org/ http://en.wikipedia.org/wiki/Nodejs http://node.codeschool.com/ http://stackoverflow.com/questions/
2353818/how-do-i-get-started-with-node-js http://code.tutsplus.com/tutorials/nodejs-
for-beginners--net-26314
References
![Page 31: Nodejs](https://reader034.vdocuments.site/reader034/viewer/2022052619/554f5f25b4c905c8088b48b9/html5/thumbnails/31.jpg)
Thank You