Skip to content
Myr

migrate.1

This file

Path

docs/migrate.1

289 lines · 5.0 KiB

Last changed by

Removed backticks from docsTj Holowaychukbefore adoptionc3f71b3

History of this file

Take it

Added migrate doc filesTj Holowaychukbefore adoption87bb27c

lines 1–6why, one step back
1.\" generated with Ronn/v0.6.6
2.\" http://github.com/rtomayko/ronn/3.4.TH "MIGRATE" "" "July 2010" "" ""5.6.SH "Migration Guide"

Removed backticks from docsTj Holowaychukbefore adoptionc3f71b3

line 7why, one step back
7Express 1\.x is written to run on\-top of the Connect \fIhttp://extjs\.github\.com/Connect\fR middlware framework, thus the \fIPlugin\fR has been replaced by Connect\'s middleware\.

Started migration docsTj Holowaychukbefore adoption9e68a5b

lines 8–30why, one step back
8.
9.SS "Creating Applications"10Previously due to legacy code implemented in the early days of node, Express unfortunately had some globals\. The DSL would previously be accessed as shown below:11.12.IP "" 413.14.nf1516require(\'express\');1718configure(function(){19 // app configuration20});2122get(\'/\', function(){23 return \'hello world\';24});25.26.fi27.28.IP "" 029.30.P

Removed backticks from docsTj Holowaychukbefore adoptionc3f71b3

line 31why, one step back
31Now we utilize the CommonJS module system appropriately, and introduce \fIexpress\.createServer()\fR which accepts the same arguments as \fIhttp\.createServer()\fR:

Started migration docsTj Holowaychukbefore adoption9e68a5b

lines 32–51why, one step back
32.
33.IP "" 434.35.nf3637var express = require(\'express\'),38 app = express\.createServer();3940app\.configure(function(){41 // app configuration42});4344app\.get(\'/\', function(req, res){45 res\.send(\'hello world\');46});47.48.fi49.50.IP "" 051.

Added view rendering migration sectionTj Holowaychukbefore adoption0b86ce2

line 52why, one step back
52.P

Docs for plugins vs middlewareTj Holowaychukbefore adoption927aae7

lines 53–70why, one step back
53Express 1\.x does \fInot\fR currently allow returning of a string\.
54.55.SS "Plugins vs Middleware"56Previously Express was bundled with plugins, which were essentially what are now Connect middleware\. Previously plugins would be utilized in a manor similar to below:57.58.IP "" 459.60.nf6162use(Logger);63use(MethodOverride);64use(Cookie);65.66.fi67.68.IP "" 069.70.P

Removed backticks from docsTj Holowaychukbefore adoptionc3f71b3

line 71why, one step back
71Which we can now \fIuse()\fR within our app, or pass to the \fIexpress\.createServer()\fR method:

Docs for plugins vs middlewareTj Holowaychukbefore adoption927aae7

lines 72–108why, one step back
72.
73.IP "" 474.75.nf7677var connect = require(\'connect\');7879var app = express\.createServer(80 connect\.logger(),81 connect\.methodOverride(),82 connect\.cookie()83);84.85.fi86.87.IP "" 088.89.P90or:91.92.IP "" 493.94.nf9596var connect = require(\'connect\');97var app = express\.createServer();9899app\.use(\'/\', connect\.logger());100app\.use(\'/\', connect\.methodOverride());101app\.use(\'/\', connect\.cookie());102.103.fi104.105.IP "" 0106.107.P108For documentation on creating Connect middleware visit Middleware Authoring \fIhttp://extjs\.github\.com/Connect/#Middleware\-Authoring\fR\.

Added view rendering migration sectionTj Holowaychukbefore adoption0b86ce2

line 109why, one step back
109.

Started migration docsTj Holowaychukbefore adoption9e68a5b

line 110why, one step back
110.SS "Running Applications"

Removed backticks from docsTj Holowaychukbefore adoptionc3f71b3

line 111why, one step back
111Previously a global function \fIrun()\fR, was available:

Started migration docsTj Holowaychukbefore adoption9e68a5b

lines 112–123why, one step back
112.
113.IP "" 4114.115.nf116117run();118.119.fi120.121.IP "" 0122.123.P

Removed backticks from docsTj Holowaychukbefore adoptionc3f71b3

line 124why, one step back
124The new \fIexpress\.Server\fR has the same API as \fIhttp\.Server\fR, so we can do things like:

Started migration docsTj Holowaychukbefore adoption9e68a5b

lines 125–135why, one step back
125.
126.IP "" 4127.128.nf129130app\.listen();131app\.listen(3000);132.133.fi134.135.IP "" 0

Migration docs for paramsTj Holowaychukbefore adoptionda84c0b

lines 136–137why, one step back
136.
137.SS "Route Parameters"

Removed backticks from docsTj Holowaychukbefore adoptionc3f71b3

line 138why, one step back
138Previously we could use \fIthis\.param()\fR to attempt fetching a route, query string, or request body parameter:

Migration docs for paramsTj Holowaychukbefore adoptionda84c0b

lines 139–165why, one step back
139.
140.IP "" 4141.142.nf143144get(\'/user/:id\', function(){145 this\.param(\'id\');146});147.148.fi149.150.IP "" 0151.152.P153Now they (\fIroute params only\fR) are passed as the third argument:154.155.IP "" 4156.157.nf158159app\.get(\'/user/:id\', function(req, res, params){160 params\.id;161});162.163.fi164.165.IP "" 0

Control passing docsTj Holowaychukbefore adoption548a070

lines 166–182why, one step back
166.
167.SS "Passing Route Control"168Old express had a weak notion of route passing, which did not support async, and was never properly implemented for practical use:169.170.IP "" 4171.172.nf173174get(\'/\', function(){175 this\.pass(\'/foobar\');176});177.178.fi179.180.IP "" 0181.182.P

Removed backticks from docsTj Holowaychukbefore adoptionc3f71b3

line 183why, one step back
183Now Express has access to Connect\'s \fInext()\fR function, which is passed as the fourth and final argument\. Calling \fInext()\fR will pass control to the next \fImatching route\fR, or continue down the stack of Connect middleware\.

Control passing docsTj Holowaychukbefore adoption548a070

lines 184–194why, one step back
184.
185.IP "" 4186.187.nf188189app\.get(\'/user/:id?\', function(req, res, params, next){190 next();191});192193app\.get(\'/user\', function(){194 // \.\.\. respond

Added view rendering migration sectionTj Holowaychukbefore adoption0b86ce2

lines 195–201why, one step back
195});
196.197.fi198.199.IP "" 0200.201.SS "View Rendering"

Removed backticks from docsTj Holowaychukbefore adoptionc3f71b3

line 202why, one step back
202View filenames no longer take the form \fINAME\fR\.\fITYPE\fR\.\fIENGINE\fR, the \fIContent\-Type\fR can be set via \fIres\.contentType()\fR or \fIres\.header()\fR\. For example what was previously \fIlayout\.html\.haml\fR, should now be \fIlayout\.haml\fR\.

Added view rendering migration sectionTj Holowaychukbefore adoption0b86ce2

lines 203–221why, one step back
203.
204.P205Previously a view render looked something like this:206.207.IP "" 4208.209.nf210211get(\'/\', function(){212 this\.render(\'index\.html\.haml\', {213 locals: { title: \'My Site\' }214 });215});216.217.fi218.219.IP "" 0220.221.P

Removed backticks from docsTj Holowaychukbefore adoptionc3f71b3

line 222why, one step back
222We now have \fIres\.render()\fR, however the options passed to haml \fIhttp://github\.com/visionmedia/haml\.js\fR, jade \fIhttp://github\.com/visionmedia/jade\fR, and others remain the same\.

Added view rendering migration sectionTj Holowaychukbefore adoption0b86ce2

lines 223–231why, one step back
223.
224.IP "" 4225.226.nf227228app\.get(\'/\', function(req, res){229 res\.render(\'index\.haml\', {230 locals: { title: \'My Site\' }231 });

Control passing docsTj Holowaychukbefore adoption548a070

lines 232–236why, one step back
232});
233.234.fi235.236.IP "" 0

Added partial migration docsTj Holowaychukbefore adoption2a674f4

lines 237–238why, one step back
237.
238.P

Removed backticks from docsTj Holowaychukbefore adoptionc3f71b3

line 239why, one step back
239Previously rendering of a collection via \fIpartial()\fR would look something like this:

Added partial migration docsTj Holowaychukbefore adoption2a674f4

lines 240–251why, one step back
240.
241.IP "" 4242.243.nf244245this\.partial(\'comment\.html\.haml\', { collection: comments });246.247.fi248.249.IP "" 0250.251.P

Removed backticks from docsTj Holowaychukbefore adoptionc3f71b3

line 252why, one step back
252Although this worked just fine, it was generally to verbose, the similar but new API looks like this, as \fIpartial()\fR is \fIalways\fR passed as a local variable:

Added partial migration docsTj Holowaychukbefore adoption2a674f4

lines 253–288why, one step back
253.
254.IP "" 4255.256.nf257258partial(\'comment\.haml\', { collection: comments });259.260.fi261.262.IP "" 0263.264.P265To make things even less verbose we can assume the extension when ommitted:266.267.IP "" 4268.269.nf270271partial(\'comment\', { collection: comments });272.273.fi274.275.IP "" 0276.277.P278And once again even further, when rendering a collection we can simply pass an array, if no other options are desired:279.280.IP "" 4281.282.nf283284partial(\'comments\', comments);285.286.fi287.288.IP "" 0

Started migration docsTj Holowaychukbefore adoption9e68a5b

line 289why, one step back
289