Skip to content
Myr

migrate.html

This file

Path

docs/migrate.html

381 lines · 10.2 KiB · HTML

Last changed by

Regenerated docs againTj Holowaychukbefore adoption8ead901

History of this file

Take it

The walk stopped after 200 commits: lines older than f3803c4 point at it.

Server#listen() docsTj Holowaychukbefore adoptionf3803c4

lines 1–99why, one step back
1<html>
2 <head>3 <title>Express - node web framework</title>4 <style>5 #tagline {6 margin-left: 75px;7 margin-bottom: 30px;8 color: rgba(255,255,255,0.7); }9 html {10 background: #1c1c1c url(images/bg.tile.jpg); }1112 body {13 margin: 0;14 padding-bottom: 30px;15 font: 14px/1.4 "Helvetica Neue", "Lucida Grande", "Arial";16 font-size: 14px;17 line-height: 1.5;18 -webkit-text-stroke: 1px rgba(0, 0, 0, 0.1);19 -moz-text-stroke: 1px rgba(0, 0, 0, 0.1);20 background: url(images/bg.jpg) 50% 0 no-repeat;21 color: #8b8b8b; }2223 * {24 outline: none; }2526 em {27 color: white; }2829 a img {30 border: none !important; }3132 a {33 font-weight: bold;34 text-decoration: none;35 color: white;36 -webkit-transition-property: opacity, -webkit-transform, color, background-color, padding, -webkit-box-shadow;37 -webkit-transition-duration: 0.15s;38 -webkit-transition-timing-function: ease-out; }39 a:hover {40 opacity: 0.8; }4142 h1, h2, h3 {43 margin: 45px 0 0 0;44 color: white;45 text-shadow: 1px 2px 2px rgba(0,0,0,0.6); }4647 h3 {48 font-size: 18px; }4950 pre {51 margin: 20px 10px;52 padding: 25px 20px;53 background: rgba(0,0,0,0.5);54 border: 1px solid #323232;55 -webkit-box-shadow: 1px 2px 2px rgba(0,0,0,0.6);56 -moz-box-shadow: 1px 2px 2px rgba(0,0,0,0.6);57 -webkit-border-radius: 5px;58 -moz-border-radius: 5px; }5960 code {61 font-family: "Helvetica Neue", "Lucida Grande", "Arial"; }6263 ul {64 margin: 15px 0;65 padding: 0 0 0 35px; }66 ul li {67 margin: 0;68 padding: 2px 0;69 list-style: square; }7071 .sect {72 margin-left: 40px; }7374 #logo {75 display: block;76 margin-left: 30%;77 margin-bottom: 30px;78 width: 194px;79 height: 51px;80 background: url(images/logo.png) 0 0 no-repeat;81 text-indent: -99999px; }82 #logo:hover {83 opacity: 0.7; }84 #logo:active {85 opacity: 0.3; }8687 #ribbon {88 position: fixed;89 top: 0;90 right: 0;91 z-index: 2; }9293 #wrapper {94 width: 100%;95 min-height: 800px;96 background: url(images/top.png) 0 0 repeat-x; }9798 #container {99 margin: 0 auto;

Regenerated docs againTj Holowaychukbefore adoption8ead901

line 100why, one step back
100 padding-top: 80px;

Server#listen() docsTj Holowaychukbefore adoptionf3803c4

lines 101–104why, one step back
101 width: 550px; }
102103 #toc {104 position: fixed;

Regenerated docs againTj Holowaychukbefore adoption8ead901

line 105why, one step back
105 top: 0;

Server#listen() docsTj Holowaychukbefore adoptionf3803c4

lines 106–113why, one step back
106 left: 0;
107 margin: 0 0 0 15px;108 padding: 15px;109 height: 100%;110 background: rgba(0,0,0,0.2);111 border-right: 1px solid rgba(255,255,255,0.05);112 }113 #toc li {

More view docsTj Holowaychukbefore adoption1c8ff48

line 114why, one step back
114 padding: 0;

Server#listen() docsTj Holowaychukbefore adoptionf3803c4

lines 115–221why, one step back
115 list-style: none;
116 }117 #toc li a {118 font-size: 11px;119 }120 #menu {121 margin-left: 65px;122 padding: 0;123 padding-bottom: 30px; }124 #menu li {125 display: inline;126 list-style: none; }127 #menu li a {128 display: block;129 float: left;130 margin: 0 2px;131 padding: 3px 15px;132 background: rgba(0,0,0,0.2);133 -webkit-border-radius: 8px;134 -moz-border-radius: 8px;135 -webkit-box-shadow: 1px 2px 2px rgba(0,0,0,0.6);136 -moz-box-shadow: 1px 2px 2px rgba(0,0,0,0.6);137 -webkit-transition-property: opacity, -webkit-transform, color, background-color, -webkit-box-shadow;138 -webkit-transition-duration: 0.15s;139 -webkit-transition-timing-function: ease-out; }140 #menu li a:hover,141 #menu li a.active {142 background: rgba(0,0,0,0.5); }143 #menu li a:active {144 background: rgba(0,0,0,0.1);145 -webkit-box-shadow: 1px 1px 1px rgba(0,0,0,0.4);146 -moz-box-shadow: 1px 1px 1px rgba(0,0,0,0.4); }147 </style>148 </head>149 <body>150 <a href='http://github.com/visionmedia/express'> 151 <img alt='Fork me on GitHub' id='ribbon' src='http://s3.amazonaws.com/github/ribbons/forkme_right_white_ffffff.png' /> 152 </a>153 <div id="wrapper">154 <div id="container">155 <a href='http://github.com/visionmedia/express' id='logo'>Express</a> 156 <p id="tagline">157 High performance, high class web development for158 <a href="http://nodejs.org">Node.js</a>159 </p>160 <ul id="menu">161 <li><a href="index.html">Home</a></li>162 <li><a href="guide.html">Guide</a></li>163 <li><a href="contrib.html">Contributing</a></li>164 <li><a href="migrate.html">1.x Migration</a></li>165 </ul>166<div class='mp'>167<h3 id="Built-On-Connect">Built On Connect</h3>168169<p>Express 1.x is written to run on-top of the <a href="http://extjs.github.com/Connect">Connect</a> middlware170framework, thus the <em>Plugin</em> has been replaced by Connect's middleware. By abstracting our middleware171to Connect we allow additional community frameworks to develop robust, high-level frameworks using172the same technologies as Express.</p>173174<h3 id="Creating-Applications">Creating Applications</h3>175176<p>Previously due to legacy code implemented in the early days of node,177Express unfortunately had some globals. The DSL would previously be178accessed as shown below:</p>179180<pre><code>require('express');181182configure(function(){183 // app configuration184});185186get('/', function(){187 return 'hello world';188});189</code></pre>190191<p>Now we utilize the CommonJS module system appropriately, and192introduce <em>express.createServer()</em> which accepts the same arguments193as <em>http.createServer()</em>:</p>194195<pre><code>var express = require('express'),196 app = express.createServer();197198app.configure(function(){199 // app configuration200});201202app.get('/', function(req, res){203 res.send('hello world');204});205</code></pre>206207<p>Express 1.x does <em>not</em> currently allow returning of a string.</p>208209<h3 id="Plugins-vs-Middleware">Plugins vs Middleware</h3>210211<p>Previously Express was bundled with plugins, which were essentially what212are now Connect middleware. Previously plugins would be utilized in a manor213similar to below:</p>214215<pre><code>use(Logger);216use(MethodOverride);217use(Cookie);218</code></pre>219220<p>Which we can now <em>use()</em> within our app, or pass to the <em>express.createServer()</em> method:</p>221

Added app.register() docsTj Holowaychukbefore adoption77e5d8e

lines 222–225why, one step back
222<pre><code>var app = express.createServer(
223 express.logger(),224 express.methodOverride(),225 express.cookieDecoder()

Server#listen() docsTj Holowaychukbefore adoptionf3803c4

lines 226–230why, one step back
226);
227</code></pre>228229<p>or:</p>230

Added app.register() docsTj Holowaychukbefore adoption77e5d8e

line 231why, one step back
231<pre><code>var app = express.createServer();

Server#listen() docsTj Holowaychukbefore adoptionf3803c4

line 232why, one step back
232

Added app.register() docsTj Holowaychukbefore adoption77e5d8e

lines 233–235why, one step back
233app.use(express.logger());
234app.use(express.methodOverride());235app.use(express.cookieDecoder());

Server#listen() docsTj Holowaychukbefore adoptionf3803c4

lines 236–265why, one step back
236</code></pre>
237238<p>For documentation on creating Connect middleware visit <a href="http://extjs.github.com/Connect/#Middleware-Authoring">Middleware Authoring</a>.</p>239240<h3 id="Running-Applications">Running Applications</h3>241242<p>Previously a global function <em>run()</em>, was available:</p>243244<pre><code>run();245</code></pre>246247<p>The new <em>express.Server</em> has the same API as <em>http.Server</em>,248so we can do things like:</p>249250<pre><code>app.listen();251app.listen(3000);252</code></pre>253254<h3 id="Route-Parameters">Route Parameters</h3>255256<p>Previously we could use <em>this.param()</em> to attempt257fetching a route, query string, or request body parameter:</p>258259<pre><code>get('/user/:id', function(){260 this.param('id');261});262</code></pre>263264<p>Polymorphic parameter access can be done using <code>req.param()</code>:</p>265

Fixed migration typoTj Holowaychukbefore adoptiona9c3f56

line 266why, one step back
266<pre><code>app.get('/user/:id', function(req, res){

Server#listen() docsTj Holowaychukbefore adoptionf3803c4

lines 267–270why, one step back
267 req.param('id');
268});269</code></pre>270

Updated docsTj Holowaychukbefore adoptiond691ecb

line 271why, one step back
271<p>Route parameters are available via <code>req.params</code>:</p>

Server#listen() docsTj Holowaychukbefore adoptionf3803c4

line 272why, one step back
272

Updated docsTj Holowaychukbefore adoptiond691ecb

lines 273–274why, one step back
273<pre><code>app.get('/user/:id', function(req, res){
274 req.params.id;

Server#listen() docsTj Holowaychukbefore adoptionf3803c4

lines 275–289why, one step back
275});
276</code></pre>277278<h3 id="Passing-Route-Control">Passing Route Control</h3>279280<p>Old express had a weak notion of route passing,281which did not support async, and was never properly282implemented for practical use:</p>283284<pre><code>get('/', function(){285 this.pass('/foobar');286});287</code></pre>288289<p>Now Express has access to Connect's <em>next()</em> function,

Updated docsTj Holowaychukbefore adoptiond691ecb

line 290why, one step back
290which is passed as the third and final argument. Calling <em>next()</em> will

Server#listen() docsTj Holowaychukbefore adoptionf3803c4

lines 291–293why, one step back
291pass control to the next <em>matching route</em>, or continue down the stack
292of Connect middleware.</p>293

Updated docsTj Holowaychukbefore adoptiond691ecb

line 294why, one step back
294<pre><code>app.get('/user/:id?', function(req, res, next){

Server#listen() docsTj Holowaychukbefore adoptionf3803c4

lines 295–339why, one step back
295 next();
296});297298app.get('/user', function(){299 // ... respond300});301</code></pre>302303<h3 id="View-Rendering">View Rendering</h3>304305<p>View filenames no longer take the form <em>Express</em>.<em>TYPE</em>.<em>ENGINE</em>,306the <em>Content-Type</em> can be set via <em>res.contentType()</em> or307<em>res.header()</em>. For example what was previously <em>layout.html.haml</em>,308should now be <em>layout.haml</em>.</p>309310<p>Previously a view render looked something like this:</p>311312<pre><code>get('/', function(){313 this.render('index.html.haml', {314 locals: { title: 'My Site' }315 });316});317</code></pre>318319<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 others320remain the same.</p>321322<pre><code>app.get('/', function(req, res){323 res.render('index.haml', {324 locals: { title: 'My Site' }325 });326});327</code></pre>328329<p>Previously rendering of a collection via <em>partial()</em> would look something like this:</p>330331<pre><code>this.partial('comment.html.haml', { collection: comments });332</code></pre>333334<p>Although this worked just fine, it was generally to verbose, the similar but new API335looks like this, as <em>partial()</em> is <em>always</em> passed as a local variable:</p>336337<pre><code>partial('comment.haml', { collection: comments });338</code></pre>339

Added more view docsTj Holowaychukbefore adoption2f96506

line 340why, one step back
340<p>To make things even less verbose we can assume the extension when omitted:</p>

Server#listen() docsTj Holowaychukbefore adoptionf3803c4

lines 341–381why, one step back
341
342<pre><code>partial('comment', { collection: comments });343</code></pre>344345<p>And once again even further, when rendering a collection we can simply pass346an array, if no other options are desired:</p>347348<pre><code>partial('comments', comments);349</code></pre>350351<h3 id="Redirecting">Redirecting</h3>352353<p>Previously you would</p>354355<pre><code>this.redirect('/somewhere');356</code></pre>357358<p>However you would now:</p>359360<pre><code>res.redirect('/somewhere');361res.redirect('/somewhere', 301);362</code></pre>363364<h3 id="HTTP-Client">HTTP Client</h3>365366<p>Previously Express provided a high level http client, this library is no more367as it does not belong in Express, however it may be resurrected as a separate module.</p>368369<h3 id="Core-Extensions">Core Extensions</h3>370371<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 to372roll your own, or install the module via:</p>373374<pre><code>$ npm install ext375</code></pre>376377</div>378 </div>379 </div>380 </body>381</html>