Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Node.js - Express and Socket.io

Node.js - Express and Socket.io

Overview of Node.js and Express Framework

Avatar for Vishal Telangre

Vishal Telangre

March 06, 2013
Tweet

More Decks by Vishal Telangre

Other Decks in Programming

Transcript

  1. Devlopers' Day | March 06, 2013 | Pune Vishal Telangre

    Vishal Telangre twitter.com/suruwat
  2. Devlopers' Day | March 06, 2013 | Pune What's for

    Today ? • Node . js • NPM • Express Framework • Socket . io • Little Jade, CoffeeScript • & an awesome Demo !
  3. Devlopers' Day | March 06, 2013 | Pune Node.js ?

    • is a platform built on Chrome's JavaScript runtime (V8) for easily building fast, scalable network applications • uses an event-driven, non-blocking I/O model • lightweight and efficient, perfect for data-intensive real-time applications that run across distributed devices • uses JavaScript as it’s primary language • http://nodejs.org
  4. Devlopers' Day | March 06, 2013 | Pune Node.js ?

    • Hello World ! var http = require('http'); http.createServer(function (req, res) { res.writeHead(200, {'Content-Type': 'text/plain'}); res.end('Hello World\n'); }).listen(3000, '127.0.0.1'); console.log('Server running at http://127.0.0.1:3000/');
  5. Devlopers' Day | March 06, 2013 | Pune Asynchronous ?

    • Node performs all operations that take time in asynchronous manner • there are no synchronous APIs built into Node • involves requesting something time consuming to occur • open / reading a file • connecting to a network socket and reading data • querying an API • once the request is made we continue on to the next line of code before waiting for the time consuming request to finish
  6. Devlopers' Day | March 06, 2013 | Pune Evented I/O

    ? • rather than using threads to scale, Node prefers preventing the main thread from being blocked in the first place • (similar to Event Machine for Ruby or Twisted for Python) • JavaScript in the browser is already based on events, Node moves them to a system level
  7. Devlopers' Day | March 06, 2013 | Pune Evented I/0

    ? var read_stream = fs.createReadStream('README.md', {encoding: 'ascii'}); read_stream.on("data", function(data){ process.stdout.write(data); }); read_stream.on("error", function(err){ console.error("An error occurred: %s", err) }); read_stream.on("close", function(){ console.log("File closed.") });
  8. Devlopers' Day | March 06, 2013 | Pune Callbacks ?

    • piece of code that should be called after an event occurs • normally receives information about the event fs.readFile('/etc/passwd', function (err, data) { if (err) throw err; console.log(data); });
  9. Devlopers' Day | March 06, 2013 | Pune Event Emitters

    • Node let you emit and listen for your own custom events • emit method takes an event name and a list for parameters • to listen for an event specify a callback to event emitter with a function that takes the passed parameters • will see later in the demo . . .
  10. Devlopers' Day | March 06, 2013 | Pune NPM ?

    • NPM – Node Packaged Modules • (similar to RubyGems in Ruby) • Simple usage: npm install -g express • -g stands for global
  11. Devlopers' Day | March 06, 2013 | Pune Modules System

    • loading system modules or NPM installed modules is so simple: • require(‘my-module’); • modules export the functionality that should be made public • Assigning properties to “exports” object • Using module.exports to export a specific object
  12. Devlopers' Day | March 06, 2013 | Pune Modules System

    var PI = Math.PI; exports.area = function (r) { return PI * r * r; }; exports.circumference = function (r) { return 2 * PI * r; }; ... var circle = require('./circle.js'); console.log( 'The area of a circle of radius 4 is ' + circle.area(4));
  13. Devlopers' Day | March 06, 2013 | Pune Express ?

    • is a minimal and flexible, light-weight REST node.js web application framework • (similar to Sinatra in Ruby) • http://expressjs.com • we've demo for it . . . express new book_shelf • don't forget to install the dependancies: cd book_shelf && npm install
  14. Devlopers' Day | March 06, 2013 | Pune BookShelf –

    An Express app • let's have a demo … • & code overview … clone code from : http://github.com/vishaltelangre/book_shelf
  15. Devlopers' Day | March 06, 2013 | Pune Express –

    Dir Structure Design • app.js entry point, configurations, dependencies • package.json third party dependencies and version numbers • public • routes controller code and URL handler • views • node_modules ...
  16. Devlopers' Day | March 06, 2013 | Pune Socket.io ?

    • great library for real-time communication between browser and server • handles the dirty work of Websockets for you • Can tie in with Express to automatically serve the client side script required
  17. Devlopers' Day | March 06, 2013 | Pune What's remain?

    • npm install coffee-script --save • downloads, saves locally inside node_modules and makes an entry in package.json • models/book.coffee • http://redis.io/commands#hash hset, hget, hgetall, hkeys, helen, hdel • admin - add books nodejs REPL • $ redis-cli hgetall 'book_shelf_development' del 'book_shelf_development' • Jade, CoffeeScript ?