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

Nodejs Intro - Part2 Introduction to Web Applications

on

  • 2,162 views

nodejs session part 2 given in Vertis tech talk.

nodejs session part 2 given in Vertis tech talk.

Statistics

Views

Total Views
2,162
Views on SlideShare
2,162
Embed Views
0

Actions

Likes
2
Downloads
24
Comments
0

0 Embeds 0

No embeds

Accessibility

Categories

Upload Details

Uploaded via as OpenOffice

Usage Rights

CC Attribution-ShareAlike LicenseCC Attribution-ShareAlike License

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

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

    Nodejs Intro - Part2 Introduction to Web Applications Nodejs Intro - Part2 Introduction to Web Applications Presentation Transcript

    • Introduction to Web Applications Budhram Gurung By - Code used for this session can be found : https://github.com/budh-ram/express_demo_app
    • node.js Created by Ryan Dahl in 2009
    • 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.
    • 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.
    • node.js can be used to build Network applications.
    • 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:
    • 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); });
    • 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.
    • Alternate Syntax var server = http.createServer(); server.on('request', function(request, response){ ... }); Event: 'close' server.on('close', function(){ ... });
    • Simple server example...
    • node.js also has frameworks...
    • 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
    • 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) { });
    • Express simple example var express = require('express'); var app = express(); app.get('/', function(req, res){ res.send('hello world'); }); app.listen(3000);
    • Express App Simple express App
    • Express App Simple express App REST urls
    • Express App Simple express App REST urls Organize your App
    • Express App Simple express App REST urls Organize your App Error handling using Error Handler Middlewares
    • Express App Simple express App REST urls Organize your App Error handling using Error Handler Middlewares Logger
    • Express App Simple express App REST urls Organize your App Error handling using Error Handler Middlewares Logger Template Engine - EJS
    • Express App Simple express App REST urls Organize your App Error handling using Error Handler Middlewares Logger Template Engine – EJS Adding Twitter Bootstrap
    • Express Middleware
    • Express Middleware bodyParser(): Request body parsing middleware supporting JSON, urlencoded, and multipart requests. Usage: app.use(express.bodyParser());
    • 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'));
    • 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());
    • 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>
    • 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 }})
    • 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.
    • Express generation Demo...
    •  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?