Nodejs Intro - Part2 Introduction to Web Applications


Published on

nodejs session part 2 given in Vertis tech talk.

Published in: Education, Technology
  • Be the first to comment

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

Nodejs Intro - Part2 Introduction to Web Applications

  1. 1. Introduction to Web Applications Budhram Gurung By - Code used for this session can be found :
  2. 2. node.js Created by Ryan Dahl in 2009
  3. 3. Background node.js runs on V8. It is a set of bindings to the V8 JavaScript VM. V8 is an open source JavaScript engine developed by Google. Its written in C++ and is used in Google Chrome Browser. Latest version is v0.10.18 Is Open Source. It runs well on Linux/Unix systems, can also run on Windows systems.
  4. 4. Installation node.js official site: Url: /Installing-Node.js-via-package-manager sudo apt-get update sudo apt-get install python-software-properties python g++ make sudo add-apt-repository ppa:chris-lea/node.js sudo apt-get update sudo apt-get install nodejs=0.10.18-1chl1~precise1 On winodws, you can use node.js installer.
  5. 5. node.js can be used to build Network applications.
  6. 6. HTTP Request Using http.request(options, callback) Otions: host, hostname, port, method, path etc var options = { hostname: '', path: '/', method: 'GET' }; var req = http.request(options, function(res) { res.on('data', function (chunk) { console.log('BODY: ' + chunk); }); }); req.end(); Example:
  7. 7. How to GET ? Using http.get(options, callback) Example: http.get("", function(res) { console.log("Got response: " + res.statusCode); }).on('error', function(e) { console.log("Got error: " + e.message); });
  8. 8. Simple http Server http.createServer(function(request, response){ ... }); http.createServer([requestListener]) Returns a new web server object. The requestListener is a function which is automatically added to the 'request' event. Class: http.Server This is an EventEmitter with the following events: Event: 'request' function (request, response) { } Emitted each time there is a request.
  9. 9. Alternate Syntax var server = http.createServer(); server.on('request', function(request, response){ ... }); Event: 'close' server.on('close', function(){ ... });
  10. 10. Simple server example...
  11. 11. node.js also has frameworks...
  12. 12. Express Sinatra inspired web development framework for Node.js It is insanely fast, flexible, and simple. Easy route URLs to callbacks Environment based configuration Middleware via connect – bodyParser, cookieparser etc Install as: npm install express Or sudo npm install -g express
  13. 13. REST url in Express Let assumes there is 'user' resource. // List app.get('/users', function(req, res) { }); // Create'/users', function(req, res) { }); // Read app.get('/users/:id', function(req, res) { }); // Update app.put('/users/:id', function(req, res) { }); // Delete app.del('/users/:id', function(req, res) { });
  14. 14. Express simple example var express = require('express'); var app = express(); app.get('/', function(req, res){ res.send('hello world'); }); app.listen(3000);
  15. 15. Express App Simple express App
  16. 16. Express App Simple express App REST urls
  17. 17. Express App Simple express App REST urls Organize your App
  18. 18. Express App Simple express App REST urls Organize your App Error handling using Error Handler Middlewares
  19. 19. Express App Simple express App REST urls Organize your App Error handling using Error Handler Middlewares Logger
  20. 20. Express App Simple express App REST urls Organize your App Error handling using Error Handler Middlewares Logger Template Engine - EJS
  21. 21. Express App Simple express App REST urls Organize your App Error handling using Error Handler Middlewares Logger Template Engine – EJS Adding Twitter Bootstrap
  22. 22. Express Middleware
  23. 23. Express Middleware bodyParser(): Request body parsing middleware supporting JSON, urlencoded, and multipart requests. Usage: app.use(express.bodyParser());
  24. 24. Express Middleware cookieParser(): Parses the Cookie header field and populates 'req.cookies' with an object keyed by the cookie names. Usage: app.use(express.cookieParser()); or app.use(express.cookieParser('some secret'));
  25. 25. Express Middleware errorHandler(): Development error handler, providing stack traces and error message responses for requests accepting text, html, or json. Usage: app.use(express.errorHandler());
  26. 26. Express Middleware methodOverride(): To simulate DELETE and PUT. Use when _method post parameter set to 'delete' or 'put'. Usage: app.use(express.methodOverride()); Client side code: <form> ... <input type="hidden" name="_method" value="put" /> </form>
  27. 27. Express Middleware session(): Setup session store with the given options. Usage: app.use(express.session()); Example: app.use(express.cookieParser()); app.use(express.session({ secret: 'keyboard cat', key: 'sid', cookie: { secure: true }})
  28. 28. Express App Generation Command to generate express app: express --sessions --css stylus --ejs myapp It generates an application with EJS, Stylus, and session. EJS: Embedded JavaScript template engine - EJS cleans the HTML out of your JavaScript with client side templates. - Similar to ERB in ruby.
  29. 29. Express generation Demo...
  30. 30.  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 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:,- Applications,-and-Companies-Using-Node Who is using Node.js?