migrate.1
This file
Path
docs/migrate.1
234 lines · 4.1 KiB
Last changed by
Added partial migration docsTj Holowaychukbefore adoption2a674f4
History of this fileTake it
1.\" generated with Ronn/v0.6.62.\" http://github.com/rtomayko/ronn/3.4.TH "MIGRATE" "" "July 2010" "" ""5.6.SH "Migration Guide"7Express 1\.x is written to run on\-top of the Connect \fIhttp://extjs\.github\.com/Connect\fR middlware framework, thus the \fBPlugin\fR has been replaced by Connect\'s middleware\.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 \fBexpress\.createServer()\fR which accepts the same arguments as \fBhttp\.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 allow returning of a string\.54.55.SS "Running Applications"56Previously a global function \fBrun()\fR, was available:57.58.IP "" 459.60.nf6162run();63.64.fi65.66.IP "" 067.68.P69The new \fBexpress\.Server\fR has the same API as \fBhttp\.Server\fR, so we can do things like:70.71.IP "" 472.73.nf7475app\.listen();76app\.listen(3000);77.78.fi79.80.IP "" 081.82.SS "Route Parameters"83Previously we could use \fBthis\.param()\fR to attempt fetching a route, query string, or request body parameter:84.85.IP "" 486.87.nf8889get(\'/user/:id\', function(){90 this\.param(\'id\');91});92.93.fi94.95.IP "" 096.97.P98Now they (\fIroute params only\fR) are passed as the third argument:99.100.IP "" 4101.102.nf103104app\.get(\'/user/:id\', function(req, res, params){105 params\.id;106});107.108.fi109.110.IP "" 0111.112.SS "Passing Route Control"113Old express had a weak notion of route passing, which did not support async, and was never properly implemented for practical use:114.115.IP "" 4116.117.nf118119get(\'/\', function(){120 this\.pass(\'/foobar\');121});122.123.fi124.125.IP "" 0126.127.P128Now Express has access to Connect\'s \fBnext()\fR function, which is passed as the fourth and final argument\. Calling \fBnext()\fR will pass control to the next \fImatching route\fR, or continue down the stack of Connect middleware\.129.130.IP "" 4131.132.nf133134app\.get(\'/user/:id?\', function(req, res, params, next){135 next();136});137138app\.get(\'/user\', function(){139 // \.\.\. respond140});141.142.fi143.144.IP "" 0145.146.SS "View Rendering"147View filenames no longer take the form \fINAME\fR\.\fITYPE\fR\.\fIENGINE\fR, the \fIContent\-Type\fR can be set via \fBServerResponse#contentType()\fR or \fBServerResponse#header()\fR\. For example what was previously \fIlayout\.html\.haml\fR, should now be \fIlayout\.haml\fR\.148.149.P150Previously a view render looked something like this:151.152.IP "" 4153.154.nf155156get(\'/\', function(){157 this\.render(\'index\.html\.haml\', {158 locals: { title: \'My Site\' }159 });160});161.162.fi163.164.IP "" 0165.166.P167We now have \fBServerResponse#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\.168.169.IP "" 4170.171.nf172173app\.get(\'/\', function(req, res){174 res\.render(\'index\.haml\', {175 locals: { title: \'My Site\' }176 });177});178.179.fi180.181.IP "" 0182.183.P184Previously rendering of a collection via \fBpartial()\fR would look something like this:185.186.IP "" 4187.188.nf189190this\.partial(\'comment\.html\.haml\', { collection: comments });191.192.fi193.194.IP "" 0195.196.P197Although this worked just fine, it was generally to verbose, the similar but new API looks like this, as \fBpartial()\fR is \fIalways\fR passed as a local variable:198.199.IP "" 4200.201.nf202203partial(\'comment\.haml\', { collection: comments });204.205.fi206.207.IP "" 0208.209.P210To make things even less verbose we can assume the extension when ommitted:211.212.IP "" 4213.214.nf215216partial(\'comment\', { collection: comments });217.218.fi219.220.IP "" 0221.222.P223And once again even further, when rendering a collection we can simply pass an array, if no other options are desired:224.225.IP "" 4226.227.nf228229partial(\'comments\', comments);230.231.fi232.233.IP "" 0234