migrate.1
This file
Path
docs/migrate.1
347 lines · 6.0 KiB
Last changed by
Rebuilt docsTj Holowaychukbefore adoption2724ce3
History of this fileTake it
The walk stopped after 200 commits: lines older than 4ee5c59 point at it.
Merge branch 'guide'Tj Holowaychukbefore adoptionf4a0809
lines 1–2why, one step back1.\" generated with Ronn/v0.7.32.\" http://github.com/rtomayko/ronn/tree/0.7.3Misc refactoring of github exampleTj Holowaychukbefore adoption4ee5c59
line 3why, one step back3.Rebuilt docsTj Holowaychukbefore adoption2724ce3
line 4why, one step back4.TH "MIGRATE" "" "October 2010" "" ""Merge branch 'guide'Tj Holowaychukbefore adoptionf4a0809
lines 5–7why, one step back5.6.SH "NAME"7\fBmigrate\fRMisc refactoring of github exampleTj Holowaychukbefore adoption4ee5c59
lines 8–80why, one step back8.9.SS "Built On Connect"10Express 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\.11.12.SS "Creating Applications"13Previously 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:14.15.IP "" 416.17.nf1819require(\'express\');2021configure(function(){22 // app configuration23});2425get(\'/\', function(){26 return \'hello world\';27});28.29.fi30.31.IP "" 032.33.P34Now we utilize the CommonJS module system appropriately, and introduce \fIexpress\.createServer()\fR which accepts the same arguments as \fIhttp\.createServer()\fR:35.36.IP "" 437.38.nf3940var express = require(\'express\'),41 app = express\.createServer();4243app\.configure(function(){44 // app configuration45});4647app\.get(\'/\', function(req, res){48 res\.send(\'hello world\');49});50.51.fi52.53.IP "" 054.55.P56Express 1\.x does \fInot\fR currently allow returning of a string\.57.58.SS "Plugins vs Middleware"59Previously Express was bundled with plugins, which were essentially what are now Connect middleware\. Previously plugins would be utilized in a manor similar to below:60.61.IP "" 462.63.nf6465use(Logger);66use(MethodOverride);67use(Cookie);68.69.fi70.71.IP "" 072.73.P74Which we can now \fIuse()\fR within our app, or pass to the \fIexpress\.createServer()\fR method:75.76.IP "" 477.78.nf7980var app = express\.createServer(Added app.register() docsTj Holowaychukbefore adoption77e5d8e
lines 81–83why, one step back81 express\.logger(),82 express\.methodOverride(),83 express\.cookieDecoder()Misc refactoring of github exampleTj Holowaychukbefore adoption4ee5c59
lines 84–98why, one step back84);85.86.fi87.88.IP "" 089.90.P91or:92.93.IP "" 494.95.nf9697var app = express\.createServer();98Added app.register() docsTj Holowaychukbefore adoption77e5d8e
lines 99–101why, one step back99app\.use(express\.logger());100app\.use(express\.methodOverride());101app\.use(express\.cookieDecoder());Misc refactoring of github exampleTj Holowaychukbefore adoption4ee5c59
lines 102–167why, one step back102.103.fi104.105.IP "" 0106.107.P108For documentation on creating Connect middleware visit Middleware Authoring \fIhttp://extjs\.github\.com/Connect/#Middleware\-Authoring\fR\.109.110.SS "Running Applications"111Previously a global function \fIrun()\fR, was available:112.113.IP "" 4114.115.nf116117run();118.119.fi120.121.IP "" 0122.123.P124The new \fIexpress\.Server\fR has the same API as \fIhttp\.Server\fR, so we can do things like:125.126.IP "" 4127.128.nf129130app\.listen();131app\.listen(3000);132.133.fi134.135.IP "" 0136.137.SS "Route Parameters"138Previously we could use \fIthis\.param()\fR to attempt fetching a route, query string, or request body parameter:139.140.IP "" 4141.142.nf143144get(\'/user/:id\', function(){145 this\.param(\'id\');146});147.148.fi149.150.IP "" 0151.152.P153Polymorphic parameter access can be done using \fBreq\.param()\fR:154.155.IP "" 4156.157.nf158159app\.get(\'/user/:id\', function(req, res){160 req\.param(\'id\');161});162.163.fi164.165.IP "" 0166.167.PUpdated docsTj Holowaychukbefore adoptiond691ecb
line 168why, one step back168Route parameters are available via \fBreq\.params\fR:Misc refactoring of github exampleTj Holowaychukbefore adoption4ee5c59
lines 169–173why, one step back169.170.IP "" 4171.172.nf173Updated docsTj Holowaychukbefore adoptiond691ecb
lines 174–175why, one step back174app\.get(\'/user/:id\', function(req, res){175 req\.params\.id;Misc refactoring of github exampleTj Holowaychukbefore adoption4ee5c59
lines 176–197why, one step back176});177.178.fi179.180.IP "" 0181.182.SS "Passing Route Control"183Old express had a weak notion of route passing, which did not support async, and was never properly implemented for practical use:184.185.IP "" 4186.187.nf188189get(\'/\', function(){190 this\.pass(\'/foobar\');191});192.193.fi194.195.IP "" 0196.197.PUpdated docsTj Holowaychukbefore adoptiond691ecb
line 198why, one step back198Now 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\.Misc refactoring of github exampleTj Holowaychukbefore adoption4ee5c59
lines 199–203why, one step back199.200.IP "" 4201.202.nf203Updated docsTj Holowaychukbefore adoptiond691ecb
line 204why, one step back204app\.get(\'/user/:id?\', function(req, res, next){Misc refactoring of github exampleTj Holowaychukbefore adoption4ee5c59
lines 205–347why, one step back205 next();206});207208app\.get(\'/user\', function(){209 // \.\.\. respond210});211.212.fi213.214.IP "" 0215.216.SS "View Rendering"217View 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\.218.219.P220Previously a view render looked something like this:221.222.IP "" 4223.224.nf225226get(\'/\', function(){227 this\.render(\'index\.html\.haml\', {228 locals: { title: \'My Site\' }229 });230});231.232.fi233.234.IP "" 0235.236.P237We 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\.238.239.IP "" 4240.241.nf242243app\.get(\'/\', function(req, res){244 res\.render(\'index\.haml\', {245 locals: { title: \'My Site\' }246 });247});248.249.fi250.251.IP "" 0252.253.P254Previously rendering of a collection via \fIpartial()\fR would look something like this:255.256.IP "" 4257.258.nf259260this\.partial(\'comment\.html\.haml\', { collection: comments });261.262.fi263.264.IP "" 0265.266.P267Although 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:268.269.IP "" 4270.271.nf272273partial(\'comment\.haml\', { collection: comments });274.275.fi276.277.IP "" 0278.279.P280To make things even less verbose we can assume the extension when omitted:281.282.IP "" 4283.284.nf285286partial(\'comment\', { collection: comments });287.288.fi289.290.IP "" 0291.292.P293And once again even further, when rendering a collection we can simply pass an array, if no other options are desired:294.295.IP "" 4296.297.nf298299partial(\'comments\', comments);300.301.fi302.303.IP "" 0304.305.SS "Redirecting"306Previously you would307.308.IP "" 4309.310.nf311312this\.redirect(\'/somewhere\');313.314.fi315.316.IP "" 0317.318.P319However you would now:320.321.IP "" 4322.323.nf324325res\.redirect(\'/somewhere\');326res\.redirect(\'/somewhere\', 301);327.328.fi329.330.IP "" 0331.332.SS "HTTP Client"333Previously 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\.334.335.SS "Core Extensions"336Express 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:337.338.IP "" 4339.340.nf341342$ npm install ext343.344.fi345.346.IP "" 0347