migrate.md
This file
Path
docs/migrate.md
130 lines · 3.3 KiB · Markdown
Last changed by
Added partial migration docsTj Holowaychukbefore adoption2a674f4
History of this fileTake it
Added migrate doc filesTj Holowaychukbefore adoption87bb27c
lines 1–3why, one step back12## Migration Guide3Started migration docsTj Holowaychukbefore adoption9e68a5b
lines 4–37why, one step back4Express 1.x is written to run on-top of the [Connect](http://extjs.github.com/Connect) middlware5framework, thus the `Plugin` has been replaced by Connect's middleware.67### 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, and24introduce `express.createServer()` which accepts the same arguments25as `http.createServer()`:2627 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 });37Added view rendering migration sectionTj Holowaychukbefore adoption0b86ce2
line 38why, one step back38Express 1.x does _not_ allow returning of a string.Started migration docsTj Holowaychukbefore adoption9e68a5b
lines 39–50why, one step back3940### Running Applications4142Previously a global function `run()`, was available:4344 run();4546The new `express.Server` has the same API as `http.Server`,47so we can do things like:4849 app.listen();50 app.listen(3000);Migration docs for paramsTj Holowaychukbefore adoptionda84c0b
lines 51–66why, one step back5152### Route Parameters5354Previously we could use `this.param()` to attempt55fetching a route, query string, or request body parameter:5657 get('/user/:id', function(){58 this.param('id');59 });6061Now they (_route params only_) are passed as the third argument:6263 app.get('/user/:id', function(req, res, params){64 params.id;65 });66Control passing docsTj Holowaychukbefore adoption548a070
lines 67–87why, one step back67### Passing Route Control6869Old express had a weak notion of route passing,70which did not support async, and was never properly 71implemented for practical use:7273 get('/', function(){74 this.pass('/foobar');75 });7677Now Express has access to Connect's `next()` function,78which is passed as the fourth and final argument. Calling `next()` will79pass control to the next _matching route_, or continue down the stack80of Connect middleware.8182 app.get('/user/:id?', function(req, res, params, next){83 next();84 });8586 app.get('/user', function(){87 // ... respondAdded view rendering migration sectionTj Holowaychukbefore adoption0b86ce2
lines 88–111why, one step back88 });8990### View Rendering9192View filenames no longer take the form _NAME_._TYPE_._ENGINE_,93the _Content-Type_ can be set via `ServerResponse#contentType()` or94`ServerResponse#header()`. For example what was previously _layout.html.haml_,95should now be _layout.haml_.9697Previously a view render looked something like this:9899 get('/', function(){100 this.render('index.html.haml', {101 locals: { title: 'My Site' }102 });103 });104105We now have `ServerResponse#render()`, however the options passed to [haml](http://github.com/visionmedia/haml.js), [jade](http://github.com/visionmedia/jade), and others106remain the same.107108 app.get('/', function(req, res){109 res.render('index.haml', {110 locals: { title: 'My Site' }111 });Control passing docsTj Holowaychukbefore adoption548a070
line 112why, one step back112 });Added partial migration docsTj Holowaychukbefore adoption2a674f4
lines 113–130why, one step back113114Previously rendering of a collection via `partial()` would look something like this:115116 this.partial('comment.html.haml', { collection: comments });117118Although this worked just fine, it was generally to verbose, the similar but new API119looks like this, as `partial()` is _always_ passed as a local variable:120121 partial('comment.haml', { collection: comments });122123To make things even less verbose we can assume the extension when ommitted:124125 partial('comment', { collection: comments });126127And once again even further, when rendering a collection we can simply pass128an array, if no other options are desired:129130 partial('comments', comments);