Skip to content
Myr

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 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–91why, one step back
86fetching a route, query string, or request body parameter:
8788 get('/user/:id', function(){89 this.param('id');90 });91

Migration docs for req.param()Tj Holowaychukbefore adoptiondc77894

lines 92–98why, one step back
92Polymorphic 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 back
99
100 app.get('/user/:id', function(req, res, params){101 params.id;102 });103

Control passing docsTj Holowaychukbefore adoption548a070

lines 104–113why, one step back
104### Passing Route Control
105106Old 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 });113

Removed backticks from docsTj Holowaychukbefore adoptionc3f71b3

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

Control passing docsTj Holowaychukbefore adoption548a070

lines 116–124why, one step back
116pass control to the next _matching route_, or continue down the stack
117of Connect middleware.118119 app.get('/user/:id?', function(req, res, params, next){120 next();121 });122123 app.get('/user', function(){124 // ... respond

Added view rendering migration sectionTj Holowaychukbefore adoption0b86ce2

lines 125–129why, one step back
125 });
126127### View Rendering128129View filenames no longer take the form _NAME_._TYPE_._ENGINE_,

Removed backticks from docsTj Holowaychukbefore adoptionc3f71b3

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

Added view rendering migration sectionTj Holowaychukbefore adoption0b86ce2

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

Removed backticks from docsTj Holowaychukbefore adoptionc3f71b3

line 142why, one step back
142We 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 143–148why, one step back
143remain 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 back
149 });

Added partial migration docsTj Holowaychukbefore adoption2a674f4

line 150why, one step back
150

Removed backticks from docsTj Holowaychukbefore adoptionc3f71b3

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

Added partial migration docsTj Holowaychukbefore adoption2a674f4

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

Removed backticks from docsTj Holowaychukbefore adoptionc3f71b3

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

Added partial migration docsTj Holowaychukbefore adoption2a674f4

lines 157–167why, one step back
157
158 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 back
168
169### 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');