Skip to content
Myr

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 file

Take it

1.\" generated with Ronn/v0.6.62.\" http://github.com/rtomayko/ronn/3.4.TH "MIGRATE" "" "August 2010" "" ""5.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(78    express\.logger(),79    express\.methodOverride(),80    express\.cookieDecoder()81);82.83.fi84.85.IP "" 086.87.P88or:89.90.IP "" 491.92.nf9394var app = express\.createServer();9596app\.use(express\.logger());97app\.use(express\.methodOverride());98app\.use(express\.cookieDecoder());99.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.nf155156app\.get(\'/user/:id\', function(req, res){157    req\.param(\'id\');158});159.160.fi161.162.IP "" 0163.164.P165Route parameters are available via \fBreq\.params\fR:166.167.IP "" 4168.169.nf170171app\.get(\'/user/:id\', function(req, res){172    req\.params\.id;173});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.P195Now 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\.196.197.IP "" 4198.199.nf200201app\.get(\'/user/:id?\', function(req, res, next){202    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.P277To make things even less verbose we can assume the extension when omitted:278.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