Upcoming SlideShare
Loading in...5




Introduction to Node.js

Introduction to Node.js



Total Views
Views on SlideShare
Embed Views



0 Embeds 0

No embeds



Upload Details

Uploaded via as Microsoft PowerPoint

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
Post Comment
Edit your comment

Nodejs Nodejs Presentation Transcript

  • Author: Bhushan Patil Date: 08 Mar 2014
  •    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 serverside. Node.js V8 JavaScript Runtime
  •   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. View slide
  •  “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) View slide
  •    A Web Framework For Beginners – it’s very low level Multi-threaded – You can think of it as single threaded server
  •  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 requestresponse paradigm.   Uses event-driven asynchronous callbacks Handles concurrency very well than other traditional web server
  • Traditional way var result = database.query("SELECT * FROM hugetable"); console.log("Hello World"); Vs Asynchronous way database.query("SELECT * FROM hugetable", function(rows) { var result = rows; }); console.log("Hello World");
  • var max var for i, a, b, c, max; = 1000000000; d =; (i = 0; i < max; i++) { a = 1234 + 5678 + i; b = 1234 * 5678 + i; c = 1234 / 2 + i; } console.log( - 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); Time comparison in milliseconds It means PHP is 93% slower than Node.js!
  •     Websocket Server - like chat application Fast File Upload Client Ad Server Any Real-Time Data Apps
  •  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.
  • Blocking code var contents = fs.readFileSync('/etc/hosts'); console.log(contents); //Stop process until complete console.log('Doing something else'); Non-Blocking code fs.readFile('/etc/hosts', function(err, contents) { console.log(contents); }); console.log('Doing something else');
  • Traditional way var result = database.query("SELECT * FROM hugetable"); console.log("Hello World"); Asynchronous way database.query("SELECT * FROM hugetable", function(rows) { var result = rows; }); console.log("Hello World");
  • var http = require('http'); http.createServer(function (req, res) { res.writeHead(200, {'Content-Type': 'text/plain'}); res.end('Hello Worldn'); }).listen(1337, ''); console.log('Server running at'); If run at command prompt: % node example.js Server running at At browser: Hello World
  •  npm, short for Node Package Manager, is two things: first and foremost, it is an online repository for the publishing of opensource 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.
  •     Some of the most popular NPM modules today are: Express - Express.js, a Sinatra-inspired web development framework for Node.js, and the defacto 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. and sockjs - Server-side component of the two most common websockets components out there today.
  •    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.
  •    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.
  • // 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);
  • And many .....
  •      8/how-do-i-get-started-with-node-js