migrate.md
This file
Path
docs/migrate.md
161 lines · 4.1 KiB · Markdown
Last changed by
Removed backticks from docsTj Holowaychukbefore adoptionc3f71b3
History of this fileTake it
Added migrate doc filesTj Holowaychukbefore adoption87bb27c
lines 1–3why, one step back12## Migration Guide3Started migration docsTj Holowaychukbefore adoption9e68a5b
line 4why, one step back4Express 1.x is written to run on-top of the [Connect](http://extjs.github.com/Connect) middlwareRemoved backticks from docsTj Holowaychukbefore adoptionc3f71b3
line 5why, one step back5framework, thus the _Plugin_ has been replaced by Connect's middleware.Started migration docsTj Holowaychukbefore adoption9e68a5b
lines 6–23why, one step back67### Creating Applications89Previously due to legacy code implemented in the early days of node,10Express unfortunately had some globals. The DSL would previously be11accessed as shown below:1213 require('express');1415 configure(function(){16 // app configuration17 });1819 get('/', function(){20 return 'hello world';21 });2223Now we utilize the CommonJS module system appropriately, andRemoved backticks from docsTj Holowaychukbefore adoptionc3f71b3
lines 24–25why, one step back24introduce _express.createServer()_ which accepts the same arguments25as _http.createServer()_:Started migration docsTj Holowaychukbefore adoption9e68a5b
lines 26–37why, one step back2627 var express = require('express'),28 app = express.createServer();29 30 app.configure(function(){31 // app configuration32 });33 34 app.get('/', function(req, res){35 res.send('hello world');36 });37Docs for plugins vs middlewareTj Holowaychukbefore adoption927aae7
lines 38–49why, one step back38Express 1.x does _not_ currently allow returning of a string.3940### Plugins vs Middleware4142Previously Express was bundled with plugins, which were essentially what43are now Connect middleware. Previously plugins would be utilized in a manor44similar to below:4546 use(Logger);47 use(MethodOverride);48 use(Cookie);49Removed backticks from docsTj Holowaychukbefore adoptionc3f71b3
line 50why, one step back50Which we can now _use()_ within our app, or pass to the _express.createServer()_ method:Docs for plugins vs middlewareTj Holowaychukbefore adoption927aae7
lines 51–69why, one step back5152 var connect = require('connect');5354 var app = express.createServer(55 connect.logger(),56 connect.methodOverride(),57 connect.cookie()58 );5960or:6162 var connect = require('connect');63 var app = express.createServer();6465 app.use('/', connect.logger());66 app.use('/', connect.methodOverride());67 app.use('/', connect.cookie());6869For documentation on creating Connect middleware visit [Middleware Authoring](http://extjs.github.com/Connect/#Middleware-Authoring).Started migration docsTj Holowaychukbefore adoption9e68a5b
lines 70–72why, one step back7071### Running Applications72Removed backticks from docsTj Holowaychukbefore adoptionc3f71b3
line 73why, one step back73Previously a global function _run()_, was available:Started migration docsTj Holowaychukbefore adoption9e68a5b
lines 74–76why, one step back7475 run();76Removed backticks from docsTj Holowaychukbefore adoptionc3f71b3
line 77why, one step back77The new _express.Server_ has the same API as _http.Server_,Started migration docsTj Holowaychukbefore adoption9e68a5b
lines 78–81why, one step back78so we can do things like:7980 app.listen();81 app.listen(3000);Migration docs for paramsTj Holowaychukbefore adoptionda84c0b
lines 82–84why, one step back8283### Route Parameters84Removed backticks from docsTj Holowaychukbefore adoptionc3f71b3
line 85why, one step back85Previously we could use _this.param()_ to attemptMigration docs for paramsTj Holowaychukbefore adoptionda84c0b
lines 86–97why, one step back86fetching a route, query string, or request body parameter:8788 get('/user/:id', function(){89 this.param('id');90 });9192Now they (_route params only_) are passed as the third argument:9394 app.get('/user/:id', function(req, res, params){95 params.id;96 });97Control passing docsTj Holowaychukbefore adoption548a070
lines 98–107why, one step back98### Passing Route Control99100Old express had a weak notion of route passing,101which did not support async, and was never properly 102implemented for practical use:103104 get('/', function(){105 this.pass('/foobar');106 });107Removed backticks from docsTj Holowaychukbefore adoptionc3f71b3
lines 108–109why, one step back108Now Express has access to Connect's _next()_ function,109which is passed as the fourth and final argument. Calling _next()_ willControl passing docsTj Holowaychukbefore adoption548a070
lines 110–118why, one step back110pass control to the next _matching route_, or continue down the stack111of Connect middleware.112113 app.get('/user/:id?', function(req, res, params, next){114 next();115 });116117 app.get('/user', function(){118 // ... respondAdded view rendering migration sectionTj Holowaychukbefore adoption0b86ce2
lines 119–123why, one step back119 });120121### View Rendering122123View filenames no longer take the form _NAME_._TYPE_._ENGINE_,Removed backticks from docsTj Holowaychukbefore adoptionc3f71b3
lines 124–125why, one step back124the _Content-Type_ can be set via _res.contentType()_ or125_res.header()_. For example what was previously _layout.html.haml_,Added view rendering migration sectionTj Holowaychukbefore adoption0b86ce2
lines 126–135why, one step back126should now be _layout.haml_.127128Previously a view render looked something like this:129130 get('/', function(){131 this.render('index.html.haml', {132 locals: { title: 'My Site' }133 });134 });135Removed backticks from docsTj Holowaychukbefore adoptionc3f71b3
line 136why, one step back136We now have _res.render()_, however the options passed to [haml](http://github.com/visionmedia/haml.js), [jade](http://github.com/visionmedia/jade), and othersAdded view rendering migration sectionTj Holowaychukbefore adoption0b86ce2
lines 137–142why, one step back137remain the same.138139 app.get('/', function(req, res){140 res.render('index.haml', {141 locals: { title: 'My Site' }142 });Control passing docsTj Holowaychukbefore adoption548a070
line 143why, one step back143 });Added partial migration docsTj Holowaychukbefore adoption2a674f4
line 144why, one step back144Removed backticks from docsTj Holowaychukbefore adoptionc3f71b3
line 145why, one step back145Previously rendering of a collection via _partial()_ would look something like this:Added partial migration docsTj Holowaychukbefore adoption2a674f4
lines 146–149why, one step back146147 this.partial('comment.html.haml', { collection: comments });148149Although this worked just fine, it was generally to verbose, the similar but new APIRemoved backticks from docsTj Holowaychukbefore adoptionc3f71b3
line 150why, one step back150looks like this, as _partial()_ is _always_ passed as a local variable:Added partial migration docsTj Holowaychukbefore adoption2a674f4
lines 151–161why, one step back151152 partial('comment.haml', { collection: comments });153154To make things even less verbose we can assume the extension when ommitted:155156 partial('comment', { collection: comments });157158And once again even further, when rendering a collection we can simply pass159an array, if no other options are desired:160161 partial('comments', comments);