Skip to content
Myr

migrate.md

This file

Path

docs/migrate.md

192 lines · 5.0 KiB · Markdown

Last changed by

Fixed typo in docsTj Holowaychukbefore adoption0a84824

History of this file

Take it

The walk stopped after 200 commits: lines older than 5842a3b point at it.

Added test for non-collection partial supportTj Holowaychukbefore adoption5842a3b

line 1why, one step back
1

Started guideTj Holowaychukbefore adoption9e3a0c6

line 2why, one step back
2### Built On Connect

Added test for non-collection partial supportTj Holowaychukbefore adoption5842a3b

line 3why, one step back
3

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

Started guideTj Holowaychukbefore adoption9e3a0c6

lines 5–7why, one step back
5framework, thus the _Plugin_ has been replaced by Connect's middleware. By abstracting our middleware
6to Connect we allow additional community frameworks to develop robust, high-level frameworks using7the same technologies as Express.

Started migration docsTj Holowaychukbefore adoption9e68a5b

lines 8–25why, one step back
8
9### Creating Applications1011Previously due to legacy code implemented in the early days of node,12Express unfortunately had some globals. The DSL would previously be13accessed as shown below:1415 require('express');1617 configure(function(){18 // app configuration19 });2021 get('/', function(){22 return 'hello world';23 });2425Now we utilize the CommonJS module system appropriately, and

Removed backticks from docsTj Holowaychukbefore adoptionc3f71b3

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

Started migration docsTj Holowaychukbefore adoption9e68a5b

lines 28–39why, one step back
28
29 var express = require('express'),30 app = express.createServer();31 32 app.configure(function(){33 // app configuration34 });35 36 app.get('/', function(req, res){37 res.send('hello world');38 });39

Docs for plugins vs middlewareTj Holowaychukbefore adoption927aae7

lines 40–51why, one step back
40Express 1.x does _not_ currently allow returning of a string.
4142### Plugins vs Middleware4344Previously Express was bundled with plugins, which were essentially what45are now Connect middleware. Previously plugins would be utilized in a manor46similar to below:4748 use(Logger);49 use(MethodOverride);50 use(Cookie);51

Removed backticks from docsTj Holowaychukbefore adoptionc3f71b3

line 52why, one step back
52Which we can now _use()_ within our app, or pass to the _express.createServer()_ method:

Docs for plugins vs middlewareTj Holowaychukbefore adoption927aae7

lines 53–58why, one step back
53
54 var connect = require('connect');5556 var app = express.createServer(57 connect.logger(),58 connect.methodOverride(),

Fixed typo in docsTj Holowaychukbefore adoption0a84824

line 59why, one step back
59 connect.cookieDecoder()

Docs for plugins vs middlewareTj Holowaychukbefore adoption927aae7

lines 60–66why, one step back
60 );
6162or:6364 var connect = require('connect');65 var app = express.createServer();66

Updated docsTj Holowaychukbefore adoptioncacada8

lines 67–69why, one step back
67 app.use(connect.logger());
68 app.use(connect.methodOverride());69 app.use(connect.cookieDecoder());

Docs for plugins vs middlewareTj Holowaychukbefore adoption927aae7

lines 70–71why, one step back
70
71For documentation on creating Connect middleware visit [Middleware Authoring](http://extjs.github.com/Connect/#Middleware-Authoring).

Started migration docsTj Holowaychukbefore adoption9e68a5b

lines 72–74why, one step back
72
73### Running Applications74

Removed backticks from docsTj Holowaychukbefore adoptionc3f71b3

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

Started migration docsTj Holowaychukbefore adoption9e68a5b

lines 76–78why, one step back
76
77 run();78

Removed backticks from docsTj Holowaychukbefore adoptionc3f71b3

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

Started migration docsTj Holowaychukbefore adoption9e68a5b

lines 80–83why, one step back
80so we can do things like:
8182 app.listen();83 app.listen(3000);

Migration docs for paramsTj Holowaychukbefore adoptionda84c0b

lines 84–86why, one step back
84
85### Route Parameters86

Removed backticks from docsTj Holowaychukbefore adoptionc3f71b3

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

Migration docs for paramsTj Holowaychukbefore adoptionda84c0b

lines 88–93why, one step back
88fetching a route, query string, or request body parameter:
8990 get('/user/:id', function(){91 this.param('id');92 });93

Migration docs for req.param()Tj Holowaychukbefore adoptiondc77894

lines 94–95why, one step back
94Polymorphic parameter access can be done using `req.param()`:
95

Fixed migration typoTj Holowaychukbefore adoptiona9c3f56

line 96why, one step back
96 app.get('/user/:id', function(req, res){

Migration docs for req.param()Tj Holowaychukbefore adoptiondc77894

lines 97–100why, one step back
97 req.param('id');
98 });99100Route parameters are also passed as the third argument:

Migration docs for paramsTj Holowaychukbefore adoptionda84c0b

lines 101–105why, one step back
101
102 app.get('/user/:id', function(req, res, params){103 params.id;104 });105

Control passing docsTj Holowaychukbefore adoption548a070

lines 106–115why, one step back
106### Passing Route Control
107108Old express had a weak notion of route passing,109which did not support async, and was never properly 110implemented for practical use:111112 get('/', function(){113 this.pass('/foobar');114 });115

Removed backticks from docsTj Holowaychukbefore adoptionc3f71b3

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

Control passing docsTj Holowaychukbefore adoption548a070

lines 118–126why, one step back
118pass control to the next _matching route_, or continue down the stack
119of Connect middleware.120121 app.get('/user/:id?', function(req, res, params, next){122 next();123 });124125 app.get('/user', function(){126 // ... respond

Added view rendering migration sectionTj Holowaychukbefore adoption0b86ce2

lines 127–131why, one step back
127 });
128129### View Rendering130131View filenames no longer take the form _NAME_._TYPE_._ENGINE_,

Removed backticks from docsTj Holowaychukbefore adoptionc3f71b3

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

Added view rendering migration sectionTj Holowaychukbefore adoption0b86ce2

lines 134–143why, one step back
134should now be _layout.haml_.
135136Previously a view render looked something like this:137138 get('/', function(){139 this.render('index.html.haml', {140 locals: { title: 'My Site' }141 });142 });143

Removed backticks from docsTj Holowaychukbefore adoptionc3f71b3

line 144why, one step back
144We 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 145–150why, one step back
145remain the same.
146147 app.get('/', function(req, res){148 res.render('index.haml', {149 locals: { title: 'My Site' }150 });

Control passing docsTj Holowaychukbefore adoption548a070

line 151why, one step back
151 });

Added partial migration docsTj Holowaychukbefore adoption2a674f4

line 152why, one step back
152

Removed backticks from docsTj Holowaychukbefore adoptionc3f71b3

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

Added partial migration docsTj Holowaychukbefore adoption2a674f4

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

Removed backticks from docsTj Holowaychukbefore adoptionc3f71b3

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

Added partial migration docsTj Holowaychukbefore adoption2a674f4

lines 159–161why, one step back
159
160 partial('comment.haml', { collection: comments });161

Added more view docsTj Holowaychukbefore adoption2f96506

line 162why, one step back
162To make things even less verbose we can assume the extension when omitted:

Added partial migration docsTj Holowaychukbefore adoption2a674f4

lines 163–169why, one step back
163
164 partial('comment', { collection: comments });165166And once again even further, when rendering a collection we can simply pass167an array, if no other options are desired:168169 partial('comments', comments);

Added redirect migration docsTj Holowaychukbefore adoption74432a6

line 170why, one step back
170

Merge branch 'redirect' into connectTj Holowaychukbefore adoption87cfca7

line 171why, one step back
171### Redirecting

Added redirect migration docsTj Holowaychukbefore adoption74432a6

lines 172–176why, one step back
172
173Previously you would174175 this.redirect('/somewhere');176

Merge branch 'redirect' into connectTj Holowaychukbefore adoption87cfca7

line 177why, one step back
177However you would now:

Added redirect migration docsTj Holowaychukbefore adoption74432a6

lines 178–179why, one step back
178
179 res.redirect('/somewhere');

Merge branch 'redirect' into connectTj Holowaychukbefore adoption87cfca7

line 180why, one step back
180 res.redirect('/somewhere', 301);

More migration docsTj Holowaychukbefore adoption38c453b

lines 181–192why, one step back
181
182### HTTP Client183184Previously Express provided a high level http client, this library is no more185as it does not belong in Express, however it may be resurrected as a separate module.186187### Core Extensions188189Express is no longer dependent on the [JavaScript Extensions](http://github.com/visionmedia/ext.js) library, so those of you using the methods provided by it such as `Object.merge(a, b)` will need to190roll your own, or install the module via:191192 $ npm install ext