Skip to content
Myr

migrate.html

This file

Path

docs/migrate.html

405 lines · 10.8 KiB · HTML

Last changed by

Merge branch 'guide'Tj Holowaychukbefore adoptionf4a0809

History of this file

Take 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		<style>6			#tagline {7			  margin-left: 75px;8			  margin-bottom: 30px;9			  color: rgba(255,255,255,0.7); }10			html {11			  background: #1c1c1c url(images/bg.tile.jpg); }1213			body {14			  margin: 0;15			  padding-bottom: 30px;16			  font: 14px/1.4 "Helvetica Neue", "Lucida Grande", "Arial";17			  font-size: 14px;18			  line-height: 1.5;19			  -webkit-text-stroke: 1px rgba(0, 0, 0, 0.1);20			  -moz-text-stroke: 1px rgba(0, 0, 0, 0.1);21			  background: url(images/bg.jpg) 50% 0 no-repeat;22			  color: #8b8b8b; }2324			* {25			  outline: none; }2627			em {28			  color: white; }2930			a img {31			  border: none !important; }3233			a {34			  font-weight: bold;35			  text-decoration: none;36			  color: white;37			  -webkit-transition-property: opacity, -webkit-transform, color, background-color, padding, -webkit-box-shadow;38			  -webkit-transition-duration: 0.15s;39			  -webkit-transition-timing-function: ease-out; }40			  a:hover {41			    opacity: 0.8; }4243			h1, h2, h3 {44			  margin: 45px 0 0 0;45			  color: white;46			  text-shadow: 1px 2px 2px rgba(0,0,0,0.6); }4748			h3 {49			  font-size: 18px; }5051			pre {52			  margin: 20px 10px;53			  padding: 25px 20px;54			  background: rgba(0,0,0,0.5);55			  border: 1px solid #323232;56			  -webkit-box-shadow: 1px 2px 2px rgba(0,0,0,0.6);57			  -moz-box-shadow: 1px 2px 2px rgba(0,0,0,0.6);58			  -webkit-border-radius: 5px;59			  -moz-border-radius: 5px; }6061			code {62			  font-family: "Helvetica Neue", "Lucida Grande", "Arial"; }6364			ul {65			  margin: 15px 0;66			  padding: 0 0 0 35px; }67			  ul li {68			    margin: 0;69			    padding: 2px 0;70			    list-style: square; }71          ul li ul {72            margin: 0;73            padding-left: 12px;74          }7576      .man-name, #Express { display:none; }7778			.sect {79			  margin-left: 40px; }8081			#logo {82			  display: block;83			  margin-left: 30%;84			  margin-bottom: 30px;85			  width: 194px;86			  height: 51px;87			  background: url(images/logo.png) 0 0 no-repeat;88			  text-indent: -99999px; }89			  #logo:hover {90			    opacity: 0.7; }91			  #logo:active {92			    opacity: 0.3; }9394			#ribbon {95			  position: fixed;96			  top: 0;97			  right: 0;98			  z-index: 2; }99100			#wrapper {101			  width: 100%;102			  min-height: 800px;103			  background: url(images/top.png) 0 0 repeat-x; }104105			#container {106			  margin: 0 auto;107			  padding-top: 80px;108			  width: 550px; }109110			#toc {111				position: fixed;112				top: 0;113				left: 0;114				margin: 0 0 0 15px;115				padding: 15px;116				height: 100%;117				background: rgba(0,0,0,0.2);118				overflow: auto;119				border-right: 1px solid rgba(255,255,255,0.05);120			}121			#toc li {122				padding: 0;123				list-style: none;124			}125			#toc li a {126				font-size: 11px;127			}128			#menu {129			  margin-left: 65px;130			  padding: 0;131			  padding-bottom: 30px; }132			  #menu li {133			    display: inline;134			    list-style: none; }135			    #menu li a {136			      display: block;137			      float: left;138			      margin: 0 2px;139			      padding: 3px 15px;140			      background: rgba(0,0,0,0.2);141			      -webkit-border-radius: 8px;142			      -moz-border-radius: 8px;143			      -webkit-box-shadow: 1px 2px 2px rgba(0,0,0,0.6);144			      -moz-box-shadow: 1px 2px 2px rgba(0,0,0,0.6);145			      -webkit-transition-property: opacity, -webkit-transform, color, background-color, -webkit-box-shadow;146			      -webkit-transition-duration: 0.15s;147			      -webkit-transition-timing-function: ease-out; }148			      #menu li a:hover,149			      #menu li a.active {150			        background: rgba(0,0,0,0.5); }151			      #menu li a:active {152			        background: rgba(0,0,0,0.1);153			        -webkit-box-shadow: 1px 1px 1px rgba(0,0,0,0.4);154			        -moz-box-shadow: 1px 1px 1px rgba(0,0,0,0.4); }155		</style>156		<script>157		  $(function(){158		    $('.section').hide();159		    $('.toggle, a.section-title').toggle(function(){160		      $(this).siblings('ul').fadeIn(300);161		      return false;162		    }, function(){163		      $(this).siblings('ul').fadeOut(300);164		      return false;165		    });166		  });167		</script>168	</head>169	<body>170		<a href='http://github.com/visionmedia/express'> 171		  <img alt='Fork me on GitHub' id='ribbon' src='http://s3.amazonaws.com/github/ribbons/forkme_right_white_ffffff.png' /> 172		</a>173		<div id="wrapper">174			<div id="container">175				<a href='http://github.com/visionmedia/express' id='logo'>Express</a> 176				<p id="tagline">177				   High performance, high class web development for178				  <a href="http://nodejs.org">Node.js</a>179				</p>180				<ul id="menu">181					<li><a href="index.html">Home</a></li>182					<li><a href="guide.html">Guide</a></li>183					<li><a href="contrib.html">Contributing</a></li>184					<li><a href="migrate.html">1.x Migration</a></li>185				</ul>186<div class='mp'>187<h2 id="Express">Express</h2>188<p class="man-name">189  <code>migrate</code>190</p>191<h3 id="Built-On-Connect">Built On Connect</h3>192193<p>Express 1.x is written to run on-top of the <a href="http://extjs.github.com/Connect">Connect</a> middlware194framework, thus the <em>Plugin</em> has been replaced by Connect's middleware. By abstracting our middleware195to Connect we allow additional community frameworks to develop robust, high-level frameworks using196the same technologies as Express.</p>197198<h3 id="Creating-Applications">Creating Applications</h3>199200<p>Previously due to legacy code implemented in the early days of node,201Express unfortunately had some globals. The DSL would previously be202accessed as shown below:</p>203204<pre><code>require('express');205206configure(function(){207    // app configuration208});209210get('/', function(){211    return 'hello world';212});213</code></pre>214215<p>Now we utilize the CommonJS module system appropriately, and216introduce <em>express.createServer()</em> which accepts the same arguments217as <em>http.createServer()</em>:</p>218219<pre><code>var express = require('express'),220    app = express.createServer();221222app.configure(function(){223    // app configuration224});225226app.get('/', function(req, res){227    res.send('hello world');228});229</code></pre>230231<p>Express 1.x does <em>not</em> currently allow returning of a string.</p>232233<h3 id="Plugins-vs-Middleware">Plugins vs Middleware</h3>234235<p>Previously Express was bundled with plugins, which were essentially what236are now Connect middleware. Previously plugins would be utilized in a manor237similar to below:</p>238239<pre><code>use(Logger);240use(MethodOverride);241use(Cookie);242</code></pre>243244<p>Which we can now <em>use()</em> within our app, or pass to the <em>express.createServer()</em> method:</p>245246<pre><code>var app = express.createServer(247    express.logger(),248    express.methodOverride(),249    express.cookieDecoder()250);251</code></pre>252253<p>or:</p>254255<pre><code>var app = express.createServer();256257app.use(express.logger());258app.use(express.methodOverride());259app.use(express.cookieDecoder());260</code></pre>261262<p>For documentation on creating Connect middleware visit <a href="http://extjs.github.com/Connect/#Middleware-Authoring">Middleware Authoring</a>.</p>263264<h3 id="Running-Applications">Running Applications</h3>265266<p>Previously a global function <em>run()</em>, was available:</p>267268<pre><code>run();269</code></pre>270271<p>The new <em>express.Server</em> has the same API as <em>http.Server</em>,272so we can do things like:</p>273274<pre><code>app.listen();275app.listen(3000);276</code></pre>277278<h3 id="Route-Parameters">Route Parameters</h3>279280<p>Previously we could use <em>this.param()</em> to attempt281fetching a route, query string, or request body parameter:</p>282283<pre><code>get('/user/:id', function(){284    this.param('id');285});286</code></pre>287288<p>Polymorphic parameter access can be done using <code>req.param()</code>:</p>289290<pre><code>app.get('/user/:id', function(req, res){291    req.param('id');292});293</code></pre>294295<p>Route parameters are available via <code>req.params</code>:</p>296297<pre><code>app.get('/user/:id', function(req, res){298    req.params.id;299});300</code></pre>301302<h3 id="Passing-Route-Control">Passing Route Control</h3>303304<p>Old express had a weak notion of route passing,305which did not support async, and was never properly306implemented for practical use:</p>307308<pre><code>get('/', function(){309    this.pass('/foobar');310});311</code></pre>312313<p>Now Express has access to Connect's <em>next()</em> function,314which is passed as the third and final argument. Calling <em>next()</em> will315pass control to the next <em>matching route</em>, or continue down the stack316of Connect middleware.</p>317318<pre><code>app.get('/user/:id?', function(req, res, next){319    next();320});321322app.get('/user', function(){323    // ... respond324});325</code></pre>326327<h3 id="View-Rendering">View Rendering</h3>328329<p>View filenames no longer take the form <em>Express</em>.<em>TYPE</em>.<em>ENGINE</em>,330the <em>Content-Type</em> can be set via <em>res.contentType()</em> or331<em>res.header()</em>. For example what was previously <em>layout.html.haml</em>,332should now be <em>layout.haml</em>.</p>333334<p>Previously a view render looked something like this:</p>335336<pre><code>get('/', function(){337    this.render('index.html.haml', {338        locals: { title: 'My Site' }339    });340});341</code></pre>342343<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 others344remain the same.</p>345346<pre><code>app.get('/', function(req, res){347    res.render('index.haml', {348        locals: { title: 'My Site' }349    });350});351</code></pre>352353<p>Previously rendering of a collection via <em>partial()</em> would look something like this:</p>354355<pre><code>this.partial('comment.html.haml', { collection: comments });356</code></pre>357358<p>Although this worked just fine, it was generally to verbose, the similar but new API359looks like this, as <em>partial()</em> is <em>always</em> passed as a local variable:</p>360361<pre><code>partial('comment.haml', { collection: comments });362</code></pre>363364<p>To make things even less verbose we can assume the extension when omitted:</p>365366<pre><code>partial('comment', { collection: comments });367</code></pre>368369<p>And once again even further, when rendering a collection we can simply pass370an array, if no other options are desired:</p>371372<pre><code>partial('comments', comments);373</code></pre>374375<h3 id="Redirecting">Redirecting</h3>376377<p>Previously you would</p>378379<pre><code>this.redirect('/somewhere');380</code></pre>381382<p>However you would now:</p>383384<pre><code>res.redirect('/somewhere');385res.redirect('/somewhere', 301);386</code></pre>387388<h3 id="HTTP-Client">HTTP Client</h3>389390<p>Previously Express provided a high level http client, this library is no more391as it does not belong in Express, however it may be resurrected as a separate module.</p>392393<h3 id="Core-Extensions">Core Extensions</h3>394395<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 to396roll your own, or install the module via:</p>397398<pre><code>$ npm install ext399</code></pre>400401</div>402			</div>403		</div>404	</body>405</html>