.\" generated with Ronn/v0.6.6 .\" http://github.com/rtomayko/ronn/ . .TH "MIGRATE" "" "July 2010" "" "" . .SS "Built On Connect" Express 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\. . .SS "Creating Applications" Previously 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: . .IP "" 4 . .nf require(\'express\'); configure(function(){ // app configuration }); get(\'/\', function(){ return \'hello world\'; }); . .fi . .IP "" 0 . .P Now we utilize the CommonJS module system appropriately, and introduce \fIexpress\.createServer()\fR which accepts the same arguments as \fIhttp\.createServer()\fR: . .IP "" 4 . .nf var express = require(\'express\'), app = express\.createServer(); app\.configure(function(){ // app configuration }); app\.get(\'/\', function(req, res){ res\.send(\'hello world\'); }); . .fi . .IP "" 0 . .P Express 1\.x does \fInot\fR currently allow returning of a string\. . .SS "Plugins vs Middleware" Previously Express was bundled with plugins, which were essentially what are now Connect middleware\. Previously plugins would be utilized in a manor similar to below: . .IP "" 4 . .nf use(Logger); use(MethodOverride); use(Cookie); . .fi . .IP "" 0 . .P Which we can now \fIuse()\fR within our app, or pass to the \fIexpress\.createServer()\fR method: . .IP "" 4 . .nf var connect = require(\'connect\'); var app = express\.createServer( connect\.logger(), connect\.methodOverride(), connect\.cookie() ); . .fi . .IP "" 0 . .P or: . .IP "" 4 . .nf var connect = require(\'connect\'); var app = express\.createServer(); app\.use(\'/\', connect\.logger()); app\.use(\'/\', connect\.methodOverride()); app\.use(\'/\', connect\.cookieDecoder()); . .fi . .IP "" 0 . .P For documentation on creating Connect middleware visit Middleware Authoring \fIhttp://extjs\.github\.com/Connect/#Middleware\-Authoring\fR\. . .SS "Running Applications" Previously a global function \fIrun()\fR, was available: . .IP "" 4 . .nf run(); . .fi . .IP "" 0 . .P The new \fIexpress\.Server\fR has the same API as \fIhttp\.Server\fR, so we can do things like: . .IP "" 4 . .nf app\.listen(); app\.listen(3000); . .fi . .IP "" 0 . .SS "Route Parameters" Previously we could use \fIthis\.param()\fR to attempt fetching a route, query string, or request body parameter: . .IP "" 4 . .nf get(\'/user/:id\', function(){ this\.param(\'id\'); }); . .fi . .IP "" 0 . .P Polymorphic parameter access can be done using \fBreq\.param()\fR: . .IP "" 4 . .nf app\.get(\'/user/:id\', function(){ req\.param(\'id\'); }); . .fi . .IP "" 0 . .P Route parameters are also passed as the third argument: . .IP "" 4 . .nf app\.get(\'/user/:id\', function(req, res, params){ params\.id; }); . .fi . .IP "" 0 . .SS "Passing Route Control" Old express had a weak notion of route passing, which did not support async, and was never properly implemented for practical use: . .IP "" 4 . .nf get(\'/\', function(){ this\.pass(\'/foobar\'); }); . .fi . .IP "" 0 . .P Now 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\. . .IP "" 4 . .nf app\.get(\'/user/:id?\', function(req, res, params, next){ next(); }); app\.get(\'/user\', function(){ // \.\.\. respond }); . .fi . .IP "" 0 . .SS "View Rendering" View 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\. . .P Previously a view render looked something like this: . .IP "" 4 . .nf get(\'/\', function(){ this\.render(\'index\.html\.haml\', { locals: { title: \'My Site\' } }); }); . .fi . .IP "" 0 . .P We 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\. . .IP "" 4 . .nf app\.get(\'/\', function(req, res){ res\.render(\'index\.haml\', { locals: { title: \'My Site\' } }); }); . .fi . .IP "" 0 . .P Previously rendering of a collection via \fIpartial()\fR would look something like this: . .IP "" 4 . .nf this\.partial(\'comment\.html\.haml\', { collection: comments }); . .fi . .IP "" 0 . .P Although 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: . .IP "" 4 . .nf partial(\'comment\.haml\', { collection: comments }); . .fi . .IP "" 0 . .P To make things even less verbose we can assume the extension when ommitted: . .IP "" 4 . .nf partial(\'comment\', { collection: comments }); . .fi . .IP "" 0 . .P And once again even further, when rendering a collection we can simply pass an array, if no other options are desired: . .IP "" 4 . .nf partial(\'comments\', comments); . .fi . .IP "" 0 . .SS "Redirect" Previously you would . .IP "" 4 . .nf this\.redirect(\'/somewhere\'); . .fi . .IP "" 0 . .P However now you should use the Redirect Middleware \fIhttp://extjs\.github\.com/Connect/redirect\.html\fR: . .IP "" 4 . .nf res\.redirect(\'/somewhere\'); . .fi . .IP "" 0