Skip to content
Myr

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 file

Take it

Added migrate doc filesTj Holowaychukbefore adoption87bb27c

lines 1–3why, one step back
1
2## Migration Guide3

Started migration docsTj Holowaychukbefore adoption9e68a5b

line 4why, one step back
4Express 1.x is written to run on-top of the [Connect](http://extjs.github.com/Connect) middlware

Removed backticks from docsTj Holowaychukbefore adoptionc3f71b3

line 5why, one step back
5framework, thus the _Plugin_ has been replaced by Connect's middleware.

Started migration docsTj Holowaychukbefore adoption9e68a5b

lines 6–23why, one step back
6
7### 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, and

Removed backticks from docsTj Holowaychukbefore adoptionc3f71b3

lines 24–25why, one step back
24introduce _express.createServer()_ which accepts the same arguments
25as _http.createServer()_:

Started migration docsTj Holowaychukbefore adoption9e68a5b

lines 26–37why, one step back
26
27 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 });37

Docs for plugins vs middlewareTj Holowaychukbefore adoption927aae7

lines 38–49why, one step back
38Express 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);49

Removed backticks from docsTj Holowaychukbefore adoptionc3f71b3

line 50why, one step back
50Which 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 back
51
52 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 back
70
71### Running Applications72

Removed backticks from docsTj Holowaychukbefore adoptionc3f71b3

line 73why, one step back
73Previously a global function _run()_, was available:

Started migration docsTj Holowaychukbefore adoption9e68a5b

lines 74–76why, one step back
74
75 run();76

Removed backticks from docsTj Holowaychukbefore adoptionc3f71b3

line 77why, one step back
77The new _express.Server_ has the same API as _http.Server_,

Started migration docsTj Holowaychukbefore adoption9e68a5b

lines 78–81why, one step back
78so we can do things like:
7980 app.listen();81 app.listen(3000);

Migration docs for paramsTj Holowaychukbefore adoptionda84c0b

lines 82–84why, one step back
82
83### Route Parameters84

Removed backticks from docsTj Holowaychukbefore adoptionc3f71b3

line 85why, one step back
85Previously we could use _this.param()_ to attempt

Migration docs for paramsTj Holowaychukbefore adoptionda84c0b

lines 86–97why, one step back
86fetching 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 });97

Control passing docsTj Holowaychukbefore adoption548a070

lines 98–107why, one step back
98### Passing Route Control
99100Old 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 });107

Removed backticks from docsTj Holowaychukbefore adoptionc3f71b3

lines 108–109why, one step back
108Now Express has access to Connect's _next()_ function,
109which is passed as the fourth and final argument. Calling _next()_ will

Control passing docsTj Holowaychukbefore adoption548a070

lines 110–118why, one step back
110pass control to the next _matching route_, or continue down the stack
111of Connect middleware.112113 app.get('/user/:id?', function(req, res, params, next){114 next();115 });116117 app.get('/user', function(){118 // ... respond

Added view rendering migration sectionTj Holowaychukbefore adoption0b86ce2

lines 119–123why, one step back
119 });
120121### View Rendering122123View filenames no longer take the form _NAME_._TYPE_._ENGINE_,

Removed backticks from docsTj Holowaychukbefore adoptionc3f71b3

lines 124–125why, one step back
124the _Content-Type_ can be set via _res.contentType()_ or
125_res.header()_. For example what was previously _layout.html.haml_,

Added view rendering migration sectionTj Holowaychukbefore adoption0b86ce2

lines 126–135why, one step back
126should 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 });135

Removed backticks from docsTj Holowaychukbefore adoptionc3f71b3

line 136why, one step back
136We now have _res.render()_, however the options passed to [haml](http://github.com/visionmedia/haml.js), [jade](http://github.com/visionmedia/jade), and others

Added view rendering migration sectionTj Holowaychukbefore adoption0b86ce2

lines 137–142why, one step back
137remain 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 back
143 });

Added partial migration docsTj Holowaychukbefore adoption2a674f4

line 144why, one step back
144

Removed backticks from docsTj Holowaychukbefore adoptionc3f71b3

line 145why, one step back
145Previously rendering of a collection via _partial()_ would look something like this:

Added partial migration docsTj Holowaychukbefore adoption2a674f4

lines 146–149why, one step back
146
147 this.partial('comment.html.haml', { collection: comments });148149Although this worked just fine, it was generally to verbose, the similar but new API

Removed backticks from docsTj Holowaychukbefore adoptionc3f71b3

line 150why, one step back
150looks like this, as _partial()_ is _always_ passed as a local variable:

Added partial migration docsTj Holowaychukbefore adoption2a674f4

lines 151–161why, one step back
151
152 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);