migrate.1
This file
Path
docs/migrate.1
344 lines · 6.0 KiB
Last changed by
Added app.register() docsTj Holowaychukbefore adoption77e5d8e
History of this fileTake it
The walk stopped after 200 commits: lines older than 136f677 point at it.
TasksTj Holowaychukbefore adoption136f677
lines 1–3why, one step back1.\" generated with Ronn/v0.6.62.\" http://github.com/rtomayko/ronn/3.Added app.register() docsTj Holowaychukbefore adoption77e5d8e
line 4why, one step back4.TH "MIGRATE" "" "August 2010" "" ""TasksTj Holowaychukbefore adoption136f677
lines 5–77why, one step back5.6.SS "Built On Connect"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\. By abstracting our middleware to Connect we allow additional community frameworks to develop robust, high\-level frameworks using the same technologies as Express\.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.P31Now we utilize the CommonJS module system appropriately, and introduce \fIexpress\.createServer()\fR which accepts the same arguments as \fIhttp\.createServer()\fR: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.52.P53Express 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.P71Which we can now \fIuse()\fR within our app, or pass to the \fIexpress\.createServer()\fR method:72.73.IP "" 474.75.nf7677var app = express\.createServer(Added app.register() docsTj Holowaychukbefore adoption77e5d8e
lines 78–80why, one step back78 express\.logger(),79 express\.methodOverride(),80 express\.cookieDecoder()TasksTj Holowaychukbefore adoption136f677
lines 81–95why, one step back81);82.83.fi84.85.IP "" 086.87.P88or:89.90.IP "" 491.92.nf9394var app = express\.createServer();95Added app.register() docsTj Holowaychukbefore adoption77e5d8e
lines 96–98why, one step back96app\.use(express\.logger());97app\.use(express\.methodOverride());98app\.use(express\.cookieDecoder());TasksTj Holowaychukbefore adoption136f677
lines 99–155why, one step back99.100.fi101.102.IP "" 0103.104.P105For documentation on creating Connect middleware visit Middleware Authoring \fIhttp://extjs\.github\.com/Connect/#Middleware\-Authoring\fR\.106.107.SS "Running Applications"108Previously a global function \fIrun()\fR, was available:109.110.IP "" 4111.112.nf113114run();115.116.fi117.118.IP "" 0119.120.P121The new \fIexpress\.Server\fR has the same API as \fIhttp\.Server\fR, so we can do things like:122.123.IP "" 4124.125.nf126127app\.listen();128app\.listen(3000);129.130.fi131.132.IP "" 0133.134.SS "Route Parameters"135Previously we could use \fIthis\.param()\fR to attempt fetching a route, query string, or request body parameter:136.137.IP "" 4138.139.nf140141get(\'/user/:id\', function(){142 this\.param(\'id\');143});144.145.fi146.147.IP "" 0148.149.P150Polymorphic parameter access can be done using \fBreq\.param()\fR:151.152.IP "" 4153.154.nf155Fixed migration typoTj Holowaychukbefore adoptiona9c3f56
line 156why, one step back156app\.get(\'/user/:id\', function(req, res){TasksTj Holowaychukbefore adoption136f677
lines 157–164why, one step back157 req\.param(\'id\');158});159.160.fi161.162.IP "" 0163.164.PUpdated docsTj Holowaychukbefore adoptiond691ecb
line 165why, one step back165Route parameters are available via \fBreq\.params\fR:TasksTj Holowaychukbefore adoption136f677
lines 166–170why, one step back166.167.IP "" 4168.169.nf170Updated docsTj Holowaychukbefore adoptiond691ecb
lines 171–172why, one step back171app\.get(\'/user/:id\', function(req, res){172 req\.params\.id;TasksTj Holowaychukbefore adoption136f677
lines 173–194why, one step back173});174.175.fi176.177.IP "" 0178.179.SS "Passing Route Control"180Old express had a weak notion of route passing, which did not support async, and was never properly implemented for practical use:181.182.IP "" 4183.184.nf185186get(\'/\', function(){187 this\.pass(\'/foobar\');188});189.190.fi191.192.IP "" 0193.194.PUpdated docsTj Holowaychukbefore adoptiond691ecb
line 195why, one step back195Now Express has access to Connect\'s \fInext()\fR function, which is passed as the third and final argument\. Calling \fInext()\fR will pass control to the next \fImatching route\fR, or continue down the stack of Connect middleware\.TasksTj Holowaychukbefore adoption136f677
lines 196–200why, one step back196.197.IP "" 4198.199.nf200Updated docsTj Holowaychukbefore adoptiond691ecb
line 201why, one step back201app\.get(\'/user/:id?\', function(req, res, next){TasksTj Holowaychukbefore adoption136f677
lines 202–276why, one step back202 next();203});204205app\.get(\'/user\', function(){206 // \.\.\. respond207});208.209.fi210.211.IP "" 0212.213.SS "View Rendering"214View 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\.215.216.P217Previously a view render looked something like this:218.219.IP "" 4220.221.nf222223get(\'/\', function(){224 this\.render(\'index\.html\.haml\', {225 locals: { title: \'My Site\' }226 });227});228.229.fi230.231.IP "" 0232.233.P234We 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\.235.236.IP "" 4237.238.nf239240app\.get(\'/\', function(req, res){241 res\.render(\'index\.haml\', {242 locals: { title: \'My Site\' }243 });244});245.246.fi247.248.IP "" 0249.250.P251Previously rendering of a collection via \fIpartial()\fR would look something like this:252.253.IP "" 4254.255.nf256257this\.partial(\'comment\.html\.haml\', { collection: comments });258.259.fi260.261.IP "" 0262.263.P264Although 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:265.266.IP "" 4267.268.nf269270partial(\'comment\.haml\', { collection: comments });271.272.fi273.274.IP "" 0275.276.PAdded more view docsTj Holowaychukbefore adoption2f96506
line 277why, one step back277To make things even less verbose we can assume the extension when omitted:TasksTj Holowaychukbefore adoption136f677
lines 278–344why, one step back278.279.IP "" 4280.281.nf282283partial(\'comment\', { collection: comments });284.285.fi286.287.IP "" 0288.289.P290And once again even further, when rendering a collection we can simply pass an array, if no other options are desired:291.292.IP "" 4293.294.nf295296partial(\'comments\', comments);297.298.fi299.300.IP "" 0301.302.SS "Redirecting"303Previously you would304.305.IP "" 4306.307.nf308309this\.redirect(\'/somewhere\');310.311.fi312.313.IP "" 0314.315.P316However you would now:317.318.IP "" 4319.320.nf321322res\.redirect(\'/somewhere\');323res\.redirect(\'/somewhere\', 301);324.325.fi326.327.IP "" 0328.329.SS "HTTP Client"330Previously Express provided a high level http client, this library is no more as it does not belong in Express, however it may be resurrected as a separate module\.331.332.SS "Core Extensions"333Express is no longer dependent on the JavaScript Extensions \fIhttp://github\.com/visionmedia/ext\.js\fR library, so those of you using the methods provided by it such as \fBObject\.merge(a, b)\fR will need to roll your own, or install the module via:334.335.IP "" 4336.337.nf338339$ npm install ext340.341.fi342.343.IP "" 0344