Share
Embed
Node.js - Express and Socket.io
Overview of Node.js and Express Framework
Vishal Telangre
March 06, 2013
Transcript
18 slides
-
Devlopers' Day | March 06, 2013 | Pune Vishal Telangre
Vishal Telangre twitter.com/suruwat -
Devlopers' Day | March 06, 2013 | Pune What's for
Today ? • Node . js • NPM • Express Framework • Socket . io • Little Jade, CoffeeScript • & an awesome Demo ! -
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 -
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/'); -
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 -
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 -
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.") }); -
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); }); -
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 . . . -
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 -
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 -
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)); -
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 -
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 -
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 ... -
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 -
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 ?