Nodejs Intro - Part2 Introduction to Web Applications
Upcoming SlideShare
Loading in...5
×

Like this? Share it with your network

Share

Nodejs Intro - Part2 Introduction to Web Applications

  • 2,445 views
Uploaded on

nodejs session part 2 given in Vertis tech talk.

nodejs session part 2 given in Vertis tech talk.

More in: Education , Technology
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Be the first to comment
No Downloads

Views

Total Views
2,445
On Slideshare
2,445
From Embeds
0
Number of Embeds
0

Actions

Shares
Downloads
27
Comments
0
Likes
2

Embeds 0

No embeds

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
    No notes for slide

Transcript

  • 1. Introduction to Web Applications Budhram Gurung By - Code used for this session can be found : https://github.com/budh-ram/express_demo_app
  • 2. node.js Created by Ryan Dahl in 2009
  • 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. Installation node.js official site: http://nodejs.org/download/ Url: https://github.com/joyent/node/wiki /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. node.js can be used to build Network applications.
  • 6. HTTP Request Using http.request(options, callback) Otions: host, hostname, port, method, path etc var options = { hostname: 'www.google.co.in', path: '/', method: 'GET' }; var req = http.request(options, function(res) { res.on('data', function (chunk) { console.log('BODY: ' + chunk); }); }); req.end(); Example:
  • 7. How to GET ? Using http.get(options, callback) Example: http.get("http://www.google.com/index.html", function(res) { console.log("Got response: " + res.statusCode); }).on('error', function(e) { console.log("Got error: " + e.message); });
  • 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. Alternate Syntax var server = http.createServer(); server.on('request', function(request, response){ ... }); Event: 'close' server.on('close', function(){ ... });
  • 10. Simple server example...
  • 11. node.js also has frameworks...
  • 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. REST url in Express Let assumes there is 'user' resource. // List app.get('/users', function(req, res) { }); // Create app.post('/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. Express simple example var express = require('express'); var app = express(); app.get('/', function(req, res){ res.send('hello world'); }); app.listen(3000);
  • 15. Express App Simple express App
  • 16. Express App Simple express App REST urls
  • 17. Express App Simple express App REST urls Organize your App
  • 18. Express App Simple express App REST urls Organize your App Error handling using Error Handler Middlewares
  • 19. Express App Simple express App REST urls Organize your App Error handling using Error Handler Middlewares Logger
  • 20. Express App Simple express App REST urls Organize your App Error handling using Error Handler Middlewares Logger Template Engine - EJS
  • 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. Express Middleware
  • 23. Express Middleware bodyParser(): Request body parsing middleware supporting JSON, urlencoded, and multipart requests. Usage: app.use(express.bodyParser());
  • 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. 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. 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. 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. 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. Express generation Demo...
  • 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 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 Who is using Node.js?