migrate.md
This file
Path
docs/migrate.md
177 lines · 4.4 KiB · Markdown
Last changed by
Migration docs for req.param()Tj Holowaychukbefore adoptiondc77894
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–91why, one step back86fetching a route, query string, or request body parameter:8788 get('/user/:id', function(){89 this.param('id');90 });91Migration docs for req.param()Tj Holowaychukbefore adoptiondc77894
lines 92–98why, one step back92Polymorphic parameter access can be done using `req.param()`:9394 app.get('/user/:id', function(){95 req.param('id');96 });9798Route parameters are also passed as the third argument:Migration docs for paramsTj Holowaychukbefore adoptionda84c0b
lines 99–103why, one step back99100 app.get('/user/:id', function(req, res, params){101 params.id;102 });103Control passing docsTj Holowaychukbefore adoption548a070
lines 104–113why, one step back104### Passing Route Control105106Old express had a weak notion of route passing,107which did not support async, and was never properly 108implemented for practical use:109110 get('/', function(){111 this.pass('/foobar');112 });113Removed backticks from docsTj Holowaychukbefore adoptionc3f71b3
lines 114–115why, one step back114Now Express has access to Connect's _next()_ function,115which is passed as the fourth and final argument. Calling _next()_ willControl passing docsTj Holowaychukbefore adoption548a070
lines 116–124why, one step back116pass control to the next _matching route_, or continue down the stack117of Connect middleware.118119 app.get('/user/:id?', function(req, res, params, next){120 next();121 });122123 app.get('/user', function(){124 // ... respondAdded view rendering migration sectionTj Holowaychukbefore adoption0b86ce2
lines 125–129why, one step back125 });126127### View Rendering128129View filenames no longer take the form _NAME_._TYPE_._ENGINE_,Removed backticks from docsTj Holowaychukbefore adoptionc3f71b3
lines 130–131why, one step back130the _Content-Type_ can be set via _res.contentType()_ or131_res.header()_. For example what was previously _layout.html.haml_,Added view rendering migration sectionTj Holowaychukbefore adoption0b86ce2
lines 132–141why, one step back132should now be _layout.haml_.133134Previously a view render looked something like this:135136 get('/', function(){137 this.render('index.html.haml', {138 locals: { title: 'My Site' }139 });140 });141Removed backticks from docsTj Holowaychukbefore adoptionc3f71b3
line 142why, one step back142We 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 143–148why, one step back143remain the same.144145 app.get('/', function(req, res){146 res.render('index.haml', {147 locals: { title: 'My Site' }148 });Control passing docsTj Holowaychukbefore adoption548a070
line 149why, one step back149 });Added partial migration docsTj Holowaychukbefore adoption2a674f4
line 150why, one step back150Removed backticks from docsTj Holowaychukbefore adoptionc3f71b3
line 151why, one step back151Previously rendering of a collection via _partial()_ would look something like this:Added partial migration docsTj Holowaychukbefore adoption2a674f4
lines 152–155why, one step back152153 this.partial('comment.html.haml', { collection: comments });154155Although this worked just fine, it was generally to verbose, the similar but new APIRemoved backticks from docsTj Holowaychukbefore adoptionc3f71b3
line 156why, one step back156looks like this, as _partial()_ is _always_ passed as a local variable:Added partial migration docsTj Holowaychukbefore adoption2a674f4
lines 157–167why, one step back157158 partial('comment.haml', { collection: comments });159160To make things even less verbose we can assume the extension when ommitted:161162 partial('comment', { collection: comments });163164And once again even further, when rendering a collection we can simply pass165an array, if no other options are desired:166167 partial('comments', comments);Added redirect migration docsTj Holowaychukbefore adoption74432a6
lines 168–177why, one step back168169### Redirect170171Previously you would172173 this.redirect('/somewhere');174175However now you should use the [Redirect Middleware](http://extjs.github.com/Connect/redirect.html):176177 res.redirect('/somewhere');