migrate.html
This file
Path
docs/migrate.html
408 lines · 10.9 KiB · HTML
Last changed by
Added app.register() docsTj Holowaychukbefore adoption77e5d8e
History of this fileTake it
1<html>2 <head>3 <title>Express - node web framework</title>4 <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>5 <script>6 $(function(){7 var done;8 $(document).scroll(function(event){9 if (!done) {10 $('#toc').animate({11 top: 012 });13 }14 });15 });16 </script>17 <style>18 #header {19 position: absolute;20 top: 10px;21 left: 0;22 padding: 12px 0;23 text-indent: 40px;24 width: 100%;25 border-top: 1px solid rgba(0,0,0,0.7);26 border-bottom: 1px solid rgba(0,0,0,0.7);27 background: rgba(255,255,255,0.1) url(http://www.sencha.com/favicon.ico) no-repeat 15px 50%;28 text-align: left;29 color: #fff;30 }31 #tagline {32 margin-left: 75px;33 margin-bottom: 30px;34 color: rgba(255,255,255,0.7); }35 html {36 background: #1c1c1c url(images/bg.tile.jpg); }3738 body {39 margin: 0;40 padding-bottom: 30px;41 font: 14px/1.4 "Helvetica Neue", "Lucida Grande", "Arial";42 font-size: 14px;43 line-height: 1.5;44 -webkit-text-stroke: 1px rgba(0, 0, 0, 0.1);45 -moz-text-stroke: 1px rgba(0, 0, 0, 0.1);46 background: url(images/bg.jpg) 50% 0 no-repeat;47 color: #8b8b8b; }4849 * {50 outline: none; }5152 em {53 color: white; }5455 a img {56 border: none !important; }5758 a {59 font-weight: bold;60 text-decoration: none;61 color: white;62 -webkit-transition-property: opacity, -webkit-transform, color, background-color, padding, -webkit-box-shadow;63 -webkit-transition-duration: 0.15s;64 -webkit-transition-timing-function: ease-out; }65 a:hover {66 opacity: 0.8; }6768 h1, h2, h3 {69 margin: 45px 0 0 0;70 color: white;71 text-shadow: 1px 2px 2px rgba(0,0,0,0.6); }7273 h3 {74 font-size: 18px; }7576 pre {77 margin: 20px 10px;78 padding: 25px 20px;79 background: rgba(0,0,0,0.5);80 border: 1px solid #323232;81 -webkit-box-shadow: 1px 2px 2px rgba(0,0,0,0.6);82 -moz-box-shadow: 1px 2px 2px rgba(0,0,0,0.6);83 -webkit-border-radius: 5px;84 -moz-border-radius: 5px; }8586 code {87 font-family: "Helvetica Neue", "Lucida Grande", "Arial"; }8889 ul {90 margin: 15px 0;91 padding: 0 0 0 35px; }92 ul li {93 margin: 0;94 padding: 2px 0;95 list-style: square; }9697 .sect {98 margin-left: 40px; }99100 #logo {101 display: block;102 margin-left: 30%;103 margin-bottom: 30px;104 width: 194px;105 height: 51px;106 background: url(images/logo.png) 0 0 no-repeat;107 text-indent: -99999px; }108 #logo:hover {109 opacity: 0.7; }110 #logo:active {111 opacity: 0.3; }112113 #ribbon {114 position: fixed;115 top: 0;116 right: 0;117 z-index: 2; }118119 #wrapper {120 width: 100%;121 min-height: 800px;122 background: url(images/top.png) 0 0 repeat-x; }123124 #container {125 margin: 0 auto;126 padding-top: 110px;127 width: 550px; }128129 #toc {130 position: fixed;131 top: 60px;132 left: 0;133 margin: 0 0 0 15px;134 padding: 15px;135 height: 100%;136 background: rgba(0,0,0,0.2);137 border-right: 1px solid rgba(255,255,255,0.05);138 }139 #toc li {140 padding: 0;141 list-style: none;142 }143 #toc li a {144 font-size: 11px;145 }146 #menu {147 margin-left: 65px;148 padding: 0;149 padding-bottom: 30px; }150 #menu li {151 display: inline;152 list-style: none; }153 #menu li a {154 display: block;155 float: left;156 margin: 0 2px;157 padding: 3px 15px;158 background: rgba(0,0,0,0.2);159 -webkit-border-radius: 8px;160 -moz-border-radius: 8px;161 -webkit-box-shadow: 1px 2px 2px rgba(0,0,0,0.6);162 -moz-box-shadow: 1px 2px 2px rgba(0,0,0,0.6);163 -webkit-transition-property: opacity, -webkit-transform, color, background-color, -webkit-box-shadow;164 -webkit-transition-duration: 0.15s;165 -webkit-transition-timing-function: ease-out; }166 #menu li a:hover,167 #menu li a.active {168 background: rgba(0,0,0,0.5); }169 #menu li a:active {170 background: rgba(0,0,0,0.1);171 -webkit-box-shadow: 1px 1px 1px rgba(0,0,0,0.4);172 -moz-box-shadow: 1px 1px 1px rgba(0,0,0,0.4); }173 </style>174 </head>175 <body>176 <a href='http://github.com/visionmedia/express'> 177 <img alt='Fork me on GitHub' id='ribbon' src='http://s3.amazonaws.com/github/ribbons/forkme_right_white_ffffff.png' /> 178 </a>179 <div id="header"><strong>Sencha</strong> labs</div>180 <div id="wrapper">181 <div id="container">182 <a href='http://github.com/visionmedia/express' id='logo'>Express</a> 183 <p id="tagline">184 High performance, high class web development for185 <a href="http://nodejs.org">Node.js</a>186 </p>187 <ul id="menu">188 <li><a href="index.html">Home</a></li>189 <li><a href="guide.html">Guide</a></li>190 <li><a href="contrib.html">Contributing</a></li>191 <li><a href="migrate.html">1.x Migration</a></li>192 </ul>193<div class='mp'>194<h3 id="Built-On-Connect">Built On Connect</h3>195196<p>Express 1.x is written to run on-top of the <a href="http://extjs.github.com/Connect">Connect</a> middlware197framework, thus the <em>Plugin</em> has been replaced by Connect's middleware. By abstracting our middleware198to Connect we allow additional community frameworks to develop robust, high-level frameworks using199the same technologies as Express.</p>200201<h3 id="Creating-Applications">Creating Applications</h3>202203<p>Previously due to legacy code implemented in the early days of node,204Express unfortunately had some globals. The DSL would previously be205accessed as shown below:</p>206207<pre><code>require('express');208209configure(function(){210 // app configuration211});212213get('/', function(){214 return 'hello world';215});216</code></pre>217218<p>Now we utilize the CommonJS module system appropriately, and219introduce <em>express.createServer()</em> which accepts the same arguments220as <em>http.createServer()</em>:</p>221222<pre><code>var express = require('express'),223 app = express.createServer();224225app.configure(function(){226 // app configuration227});228229app.get('/', function(req, res){230 res.send('hello world');231});232</code></pre>233234<p>Express 1.x does <em>not</em> currently allow returning of a string.</p>235236<h3 id="Plugins-vs-Middleware">Plugins vs Middleware</h3>237238<p>Previously Express was bundled with plugins, which were essentially what239are now Connect middleware. Previously plugins would be utilized in a manor240similar to below:</p>241242<pre><code>use(Logger);243use(MethodOverride);244use(Cookie);245</code></pre>246247<p>Which we can now <em>use()</em> within our app, or pass to the <em>express.createServer()</em> method:</p>248249<pre><code>var app = express.createServer(250 express.logger(),251 express.methodOverride(),252 express.cookieDecoder()253);254</code></pre>255256<p>or:</p>257258<pre><code>var app = express.createServer();259260app.use(express.logger());261app.use(express.methodOverride());262app.use(express.cookieDecoder());263</code></pre>264265<p>For documentation on creating Connect middleware visit <a href="http://extjs.github.com/Connect/#Middleware-Authoring">Middleware Authoring</a>.</p>266267<h3 id="Running-Applications">Running Applications</h3>268269<p>Previously a global function <em>run()</em>, was available:</p>270271<pre><code>run();272</code></pre>273274<p>The new <em>express.Server</em> has the same API as <em>http.Server</em>,275so we can do things like:</p>276277<pre><code>app.listen();278app.listen(3000);279</code></pre>280281<h3 id="Route-Parameters">Route Parameters</h3>282283<p>Previously we could use <em>this.param()</em> to attempt284fetching a route, query string, or request body parameter:</p>285286<pre><code>get('/user/:id', function(){287 this.param('id');288});289</code></pre>290291<p>Polymorphic parameter access can be done using <code>req.param()</code>:</p>292293<pre><code>app.get('/user/:id', function(req, res){294 req.param('id');295});296</code></pre>297298<p>Route parameters are available via <code>req.params</code>:</p>299300<pre><code>app.get('/user/:id', function(req, res){301 req.params.id;302});303</code></pre>304305<h3 id="Passing-Route-Control">Passing Route Control</h3>306307<p>Old express had a weak notion of route passing,308which did not support async, and was never properly309implemented for practical use:</p>310311<pre><code>get('/', function(){312 this.pass('/foobar');313});314</code></pre>315316<p>Now Express has access to Connect's <em>next()</em> function,317which is passed as the third and final argument. Calling <em>next()</em> will318pass control to the next <em>matching route</em>, or continue down the stack319of Connect middleware.</p>320321<pre><code>app.get('/user/:id?', function(req, res, next){322 next();323});324325app.get('/user', function(){326 // ... respond327});328</code></pre>329330<h3 id="View-Rendering">View Rendering</h3>331332<p>View filenames no longer take the form <em>Express</em>.<em>TYPE</em>.<em>ENGINE</em>,333the <em>Content-Type</em> can be set via <em>res.contentType()</em> or334<em>res.header()</em>. For example what was previously <em>layout.html.haml</em>,335should now be <em>layout.haml</em>.</p>336337<p>Previously a view render looked something like this:</p>338339<pre><code>get('/', function(){340 this.render('index.html.haml', {341 locals: { title: 'My Site' }342 });343});344</code></pre>345346<p>We now have <em>res.render()</em>, however the options passed to <a href="http://github.com/visionmedia/haml.js">haml</a>, <a href="http://github.com/visionmedia/jade">jade</a>, and others347remain the same.</p>348349<pre><code>app.get('/', function(req, res){350 res.render('index.haml', {351 locals: { title: 'My Site' }352 });353});354</code></pre>355356<p>Previously rendering of a collection via <em>partial()</em> would look something like this:</p>357358<pre><code>this.partial('comment.html.haml', { collection: comments });359</code></pre>360361<p>Although this worked just fine, it was generally to verbose, the similar but new API362looks like this, as <em>partial()</em> is <em>always</em> passed as a local variable:</p>363364<pre><code>partial('comment.haml', { collection: comments });365</code></pre>366367<p>To make things even less verbose we can assume the extension when omitted:</p>368369<pre><code>partial('comment', { collection: comments });370</code></pre>371372<p>And once again even further, when rendering a collection we can simply pass373an array, if no other options are desired:</p>374375<pre><code>partial('comments', comments);376</code></pre>377378<h3 id="Redirecting">Redirecting</h3>379380<p>Previously you would</p>381382<pre><code>this.redirect('/somewhere');383</code></pre>384385<p>However you would now:</p>386387<pre><code>res.redirect('/somewhere');388res.redirect('/somewhere', 301);389</code></pre>390391<h3 id="HTTP-Client">HTTP Client</h3>392393<p>Previously Express provided a high level http client, this library is no more394as it does not belong in Express, however it may be resurrected as a separate module.</p>395396<h3 id="Core-Extensions">Core Extensions</h3>397398<p>Express is no longer dependent on the <a href="http://github.com/visionmedia/ext.js">JavaScript Extensions</a> library, so those of you using the methods provided by it such as <code>Object.merge(a, b)</code> will need to399roll your own, or install the module via:</p>400401<pre><code>$ npm install ext402</code></pre>403404</div>405 </div>406 </div>407 </body>408</html>