EXPRESS
                             web framework for node




Aaron Heckmann
aaronheckmann@gmail.com                                     Area 15
http://twitter.com/aaronheckmann                      April 15 2010
HISTORY
 (abridged)
GMAIL
CHROME
V8
RYAN DAHL
SERVERS
NGINX
EVENT LOOP
JAVASCRIPT
SPEED
V8
evented I/O for V8 javascript
ASYNC I/O
<?php
// I block!
$data = file_get_contents($file)
// do stuff with $data
?>
ASYNC I/O
<?php
// I block!
$data = file_get_contents($file)
// do stuff with $data
?>



jQuery.ajax({
   success: function(data) {
     // do something with data
   }
})
ASYNC I/O
<?php
// I block!
$data = file_get_contents($file)
// do stuff with $data
?>



jQuery.ajax({
   success: function(data) {
     // do something with data
   }
})



// I don’t block!
var fs = require('fs')
fs.readFile(file, function(err, data) {
   // i can has data
})
REQUIRE
• Node     uses the commonJS module system

• returns    the exported API of a module

// math.js
exports.add = function() {
   var sum = arguments[0];
   for (var i=1; i<arguments.length; i++) {
     sum += arguments[i];
   }
   return sum;
};

// app.js
var add = require('math').add;
add(4, 8, 15, 16, 23, 42); //-> 108
FAMILIAR

• addListener()

• removeListener()

• setTimeout()

• setInterval()
LOW LEVEL

• HTTP     / TCP / DNS

• file   system

• child   processes

• event   emitters

• and   streams, oh my!
• lots

• lots

• more
EXPRESS

• simple   install

• built   on node

• async   rendering

• plugable
SIMPLE INSTALL

$ git clone git://github.com/visionmedia/express.git
$ cd express && git submodule update --init


                               or
$ kiwi -v install express
BUILT ON NODE
// app.js

require.paths.unshift('path/to/express/lib')
require('express')

get('/', function(){
   this.redirect('/hello/world')
})

get('/hello/world', function(){
   return 'Hello World'
})

run()




$ node app.js
Express started at http://localhost:8000/ in development mode
ASYNC RENDERING


get('/', function(){
   return '<!doctype html><html><head><title>...'
})
ASYNC RENDERING


get('/', function() {
   var self = this // => Request
})
ASYNC RENDERING

get('/', function() {
   var self = this
   db.get(someUserId, function(err, user) {
      if (err) return self.error(err);
   })
})
ASYNC RENDERING
get('/', function() {
   var self = this
   db.get(someUserId, function(err, user) {
      if (err) return self.error(err);
      self.render('home.html.haml', {
         layout: 'layout-public’,
         locals: {
           title: 'intro to express',
           firstname: user.firstname
         }
      })
   })
})
#wrap
  .welcome
    %p= “Hello ” + firstname + “!”




<div id=“wrap”>
  <div class=“welcome”>
    <p>Hello Aaron!</p>
  </div>
</div>
.welcome
  :margin 5px
  p
    :color #2C7C00
    :text-shadow 1px 2px 1px #ddd




.welcome {
  margin: 5px; }
.welcome p {
  color: #2C7C00;
  text-shadow: 1px 2px 1px #ddd; }
PLUGABLE
PLUGABLE

• View, Cache, Redirect, etc   (defaults)

• Cookie, Session, Flash, Logger, Static, etc   (optional)

• 3rd   party session persistence (MongoDB, Redis)

• Async!
PLUGABLE

var Logger = require('express/plugins/logger').Logger
use(Logger, {format: 'combined'})
MOAR

• partials   & flash messaging

• custom     error pages (500, 404)

• http   (async curl)

• uploads

• custom     environment configurations

• ES5    (v8)
THANK YOU
•   http://expressjs.com/

•   http://groups.google.com/group/express-js

•   http://github.com/visionmedia/express

•   http://nodejs.org/

•   http://groups.google.com/group/nodejs

•   http://wiki.github.com/ry/node/modules

•   http://twitter.com/expressjs

•   http://twitter.com/aaronheckmann

Express Presentation

  • 1.
    EXPRESS web framework for node Aaron Heckmann aaronheckmann@gmail.com Area 15 http://twitter.com/aaronheckmann April 15 2010
  • 2.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
    evented I/O forV8 javascript
  • 15.
    ASYNC I/O <?php // Iblock! $data = file_get_contents($file) // do stuff with $data ?>
  • 16.
    ASYNC I/O <?php // Iblock! $data = file_get_contents($file) // do stuff with $data ?> jQuery.ajax({ success: function(data) { // do something with data } })
  • 17.
    ASYNC I/O <?php // Iblock! $data = file_get_contents($file) // do stuff with $data ?> jQuery.ajax({ success: function(data) { // do something with data } }) // I don’t block! var fs = require('fs') fs.readFile(file, function(err, data) { // i can has data })
  • 19.
    REQUIRE • Node uses the commonJS module system • returns the exported API of a module // math.js exports.add = function() { var sum = arguments[0]; for (var i=1; i<arguments.length; i++) { sum += arguments[i]; } return sum; }; // app.js var add = require('math').add; add(4, 8, 15, 16, 23, 42); //-> 108
  • 20.
  • 21.
    LOW LEVEL • HTTP / TCP / DNS • file system • child processes • event emitters • and streams, oh my!
  • 22.
  • 24.
    EXPRESS • simple install • built on node • async rendering • plugable
  • 25.
    SIMPLE INSTALL $ gitclone git://github.com/visionmedia/express.git $ cd express && git submodule update --init or $ kiwi -v install express
  • 26.
    BUILT ON NODE //app.js require.paths.unshift('path/to/express/lib') require('express') get('/', function(){ this.redirect('/hello/world') }) get('/hello/world', function(){ return 'Hello World' }) run() $ node app.js Express started at http://localhost:8000/ in development mode
  • 27.
    ASYNC RENDERING get('/', function(){ return '<!doctype html><html><head><title>...' })
  • 28.
    ASYNC RENDERING get('/', function(){ var self = this // => Request })
  • 29.
    ASYNC RENDERING get('/', function(){ var self = this db.get(someUserId, function(err, user) { if (err) return self.error(err); }) })
  • 30.
    ASYNC RENDERING get('/', function(){ var self = this db.get(someUserId, function(err, user) { if (err) return self.error(err); self.render('home.html.haml', { layout: 'layout-public’, locals: { title: 'intro to express', firstname: user.firstname } }) }) })
  • 32.
    #wrap .welcome %p= “Hello ” + firstname + “!” <div id=“wrap”> <div class=“welcome”> <p>Hello Aaron!</p> </div> </div>
  • 34.
    .welcome :margin5px p :color #2C7C00 :text-shadow 1px 2px 1px #ddd .welcome { margin: 5px; } .welcome p { color: #2C7C00; text-shadow: 1px 2px 1px #ddd; }
  • 35.
  • 36.
    PLUGABLE • View, Cache,Redirect, etc (defaults) • Cookie, Session, Flash, Logger, Static, etc (optional) • 3rd party session persistence (MongoDB, Redis) • Async!
  • 37.
    PLUGABLE var Logger =require('express/plugins/logger').Logger use(Logger, {format: 'combined'})
  • 38.
    MOAR • partials & flash messaging • custom error pages (500, 404) • http (async curl) • uploads • custom environment configurations • ES5 (v8)
  • 39.
    THANK YOU • http://expressjs.com/ • http://groups.google.com/group/express-js • http://github.com/visionmedia/express • http://nodejs.org/ • http://groups.google.com/group/nodejs • http://wiki.github.com/ry/node/modules • http://twitter.com/expressjs • http://twitter.com/aaronheckmann